Reputation: 177
I have data frame with many columns. While execution i got an error like
Can only use .str accessor with string values, which use np.object_ dtype in pandas
I figured it out this is because one my column has only Nan values on that particular occasion and the .str failed on the code
df1['Merchant_old']=df1['Merchant_old'].str.lower().str.strip()
I don't how I should use isna() to run the above step only when the entire column is not blank.
Upvotes: 1
Views: 73
Reputation: 11602
I assume that type of your df.col
is np.float
. You could get away with using str
with df.col.astype(np.object_).str
. This will keep np.NaN
s in the final output.
Upvotes: 2
Reputation: 323226
You can create a mask here, with isnull
and all
df=pd.DataFrame({'A':['A',np.nan,'B'],'B':[np.nan,np.nan,np.nan]})
s=df.isnull().all()
df.loc[:,s[~s].index]=df.loc[:,s[~s].index].apply(lambda x : x.str.lower().str.strip())
df
A B
0 a NaN
1 NaN NaN
2 b NaN
Upvotes: 1