1

Im trying to replace an empty string " " in a pandas dataframe column. The dtype of that column is "O". Ive tried using

df[col].replace(" ", np.nan, inplace = True)

However, I get a nonetype object in return or the whole column is erased. I've also tried using

df[col].dropna(axis=0, subset=['CLOSED_TIME'])

I should also mention the column is a bunch of dates/time but not in datetime format. My goal is to fill the empty strings with nan, so that i can run .fillna(0) on the whole dataframe.

Code Monkey
  • 59
  • 1
  • 12
  • 1
    The first line should work. I would suggest pasting the entire traceback – Chris Jan 09 '20 at 17:03
  • Are you sure you aren't doing `df[col] = ... inplace=True)` You shouldn't assign **and** specify an inplace operation – ALollz Jan 09 '20 at 17:08
  • Does this answer your question? [replacing empty strings with NaN in Pandas](https://stackoverflow.com/questions/40711900/replacing-empty-strings-with-nan-in-pandas) – Umar.H Jan 09 '20 at 17:16
  • @Chris it won't work if the spaces are of variable len. – Umar.H Jan 09 '20 at 17:16

1 Answers1

2

try this df[col].replace(r'^\s*$', np.nan, regex=True)

Ayoub Benayache
  • 1,046
  • 12
  • 28