I am sorry if this is a duplicate question, I did hunt around a bit before I felt like I had to post a question.
I am trying to assign a value in a new column devicevalue based on a value of another 2 columns. My dataframe looks a bit like this;
devicename make devicevalue
switch1 cisco 0
switch1-web100 netgear 0
switch10 cisco 0
switch23 cisco 1
switch31-web200 netgear 0
switch31 cisco 1
switch41-new cisco 1
switch40e cisco 1
switch31-web200-new netgear 0
switch40e cisco 1
switch11-data100e netgear 0
I am trying to add a value depending on these criteria;
- If
make == netgear(set to 0) - If the value after switch is 20 or greater (set to 1, otherwise set to 0)
(If both conditions met, set to 0, i.e. condition of "make == netgear set to 0" takes precedence. Note that this is different from the existing codes where the 2nd condition override (and overwrite result value) if both conditions met.)
I originally had some help getting this together however some devices now have a -new and por a or e which breaks the code that looking at a number at the end of the string
The code I am using is essentially;
def get_number_suffix(devicename: str) -> int:
i = 1
while i < len(devicename) and devicename[-i:].isnumeric():
i += 1
return int(devicename[-(i-1):])
def compute_devicevalue(row) -> int:
if 'netgear' in row['make']:
return 0
if 20 <= get_number_suffix(row['devicename']):
return 1
else:
return 0
df['devicevalue'] = df.apply(compute_devicevalue, axis=1)
this worked fine before the new additions to the end of some of the naming, now it obviously breaks.
I have tried all sorts of ways but I can't find a decent way that ignores -new and por a or e
edit
Sorry all, I completely messed up what I was trying to ask, I'm trying to do the value based on the value after 'switch'.
Essentially using the existing code when it converts the string to an integer and does len it falls over on any name that has a -new and por a or e following it
as an example saying
ValueError: invalid literal for int() with base 10: 'switch23-new'