Pandas: AttributeError: 'str' object has no attribute 'isnull' - python

I'm creating a new column named lead_actor_actress_known whose values is boolean based on whether there value in 2nd column lead_actor_actress has value or not. If there is a value(Name of actors) populate 1st column using True if there is no value, return False
AttributeError: 'str' object has no attribute 'isnull'
My code is throwing an error above.
df['lead_actor_actress_known'] = df['lead_actor_actress'].apply(lambda x: True if x.isnull() else False)
What i'm i missing?

Henry Ecker's comment contains the answer to this question, I am reproducing in the answer section for convenience. Replace your application of the .apply() method with the code df['lead_actor_actress_known'] = df['lead_actor_actress'].isna().
The thing to know here is that df['lead_actor_actress'].isna() returns a "Boolean mask" (a series of True and False values) and this is exactly what you are asking to assign the variable lead_actor_actress.

Related

AttributeError- 'numpy.ndarray' object has no attribute 'index'

I keep getting this error message:
newmaxleft=cl1count.index(max(cl1count))
AttributeError: 'numpy.ndarray' object has no attribute 'index'
The purpose of the code is to find the first occurrents of the column with the largest amount of white pixels.
My Code:
cl = cl[top:bottom, left:right]
cl1mask = np.uint8(np.where(cl == 0, 0, 1))
cl1count = cv2.reduce(cl1mask, 0, cv2.REDUCE_SUM, dtype=cv2.CV_32SC1)
cl1count.flatten().tolist()
newmaxleft=cl1count.index(max(cl1count))
Numpy doesn't have index method. It uses where (for general purposes) or some specific functions. In your case the best choice is
newmaxleft = cl1count.argmax()
You can also use
newmaxleft = np.where(a==max(a))[0][0]
But it's less efficient. First [0] returns np.array of positions, second [0] returns first occurrence.
The error happens because numpy arrays don't have the index attribute, as the error says. So instead of using index, use something like newmaxleft=np.where(cl1count == max(cl1count)) where you can use newmaxleft[0] to get the first occurrence. You could also use argmax.

appending to list within list gives AttributeError: 'int' object has no attribute 'append'

when executing the following code, I get the error message "AttributeError: 'int' object has no attribute 'append'". I know this error occurs when I try to append to an integer, for example, which is not the case here since I checked that via type(section[len(section)-1]) which returned list
def increasing_section(a_list):
section = [[a_list[0]]]
i = 1
while i < len(a_list):
if a_list[i-1] < a_list[i]:
section[len(section)-1].append(a_list[i])
else:
section.append(a_list[i])
i += 1
return section
The error comes from this line:
section[len(section)-1].append(a_list[i])
because at some point, when your if condition will not be met, the following line:
section.append(a_list[i])
will add an integer in section and the first line will produce error as section[len(section)-1] will be an integer
I'm not sure what exactly you are trying to do, but to fix this you need to change section[len(section)-1].append(a_list[i]). Because section[len(section)-1] will return an integer and integer doesn't have append method, so if you want to insert an element to a list at a particular index, try section.insert(index, value)

check when trying to assign Boolean

ctx['location_ids'] = vals['location_ids']
I have a large function so I will not post it here, but the problem is when vals['location_ids'] have values as integer everything works smooth, but sometimes there are no values in vals['location_ids'] so it is False, and when it is False I get error.
ctx['location_ids'] = vals['location_ids']
TypeError: 'bool' object has no attribute '__getitem__'
how can I avoid it, maybe add hasattr?
you should try to check first it's dictionary
if isinstance(vals, dict):
ctx['location_ids'] = vals.get('location_ids', None)

Using len() with if/else to populate dataframe variable

I am looking to add a variable to my dataframe that concatenates several other variables. I know that if variable 'do' is less that 4 characters, it is garbage input and I should instead use variable 'ra'. However, the below throws an attribute error: "AttributeError: ("'str' object has no attribute 'len'", 'occurred at index 0')". Is the apply operation the correct way to go about what I'm doing, and if so, how can I correct my function?
def get_combined(row):
if row['do'].len() < 4:
return row['ra']+' '+row['mi']+' '+row['fa']+' '+row['so']
else:
return row['do']+' '+row['mi']+' '+row['fa']+' '+row['so']
df['Combined'] = df.apply(get_combined, axis=1)
The second line should be:
if len(row['do']) < 4:

Pandas converting String object to lower case and checking for string

I have the below code
import pandas as pd
private = pd.read_excel("file.xlsx","Pri")
public = pd.read_excel("file.xlsx","Pub")
private["ISH"] = private.HolidayName.str.lower().contains("holiday|recess")
public["ISH"] = public.HolidayName.str.lower().contains("holiday|recess")
I get the following error:
AttributeError: 'Series' object has no attribute 'contains'
Is there anyway to convert the 'HolidayName' column to lower case and then check the regular expression ("Holiday|Recess")using .contains in one step?
private["ISH"] = private.HolidayName.str.contains("(?i)holiday|recess")
The (?i) in the regex pattern tells the re module to ignore case.
The reason why you were getting an error is because the Series object does not have the contains method; instead the Series.str attribute has the contains method. So you could avoid the error with:
private["ISH"] = private.HolidayName.str.lower().str.contains("holiday|recess")
I'm a bit late to the party, but you could use the keyarg
case : bool, default True, If True, case sensitive.
private["ISH"] = private.HolidayName.str.contains("holiday|recess", case=False)
public["ISH"] = public.HolidayName.str.contains("holiday|recess", case=False)

Categories

Resources