I have a pandas script where I get an excel sheet and put it on a pandas dataframe, then I am looking in this dataframe for a specific word, then I create a mask of 1 and 0 of the df, where I find the word.
I don't have a specific format for the excel sheet so I get all the info as is, and I look for the word and create a mask with this line which produce the error:
mask = np.column_stack([df[col].str.find(word) for col in df.columns.tolist()]).astype(int)
this line sometimes produce this error:
pandas can only use .str accessor with string values, which use np.object_ dtype in pandas
any idea why and how to make it work?
thank you
df.select_dtypes([np.object]).columns.tolist()instead ofdf.columns.tolist()in your list comprehension. Right now, you are selecting all thedtypesof columns which could very well be mixed. You need to confine this to only the string ones forstr.find()function to work properly.