Home > OS >  Handle NaN values in column TypeError: replace() argument 1 must be str, not float
Handle NaN values in column TypeError: replace() argument 1 must be str, not float

Time:09-10

df = pd.DataFrame({
  "string": ["foo", "moo", "too"],
  "substring": ["oo", "m", np.nan]
})
df['string'] = [a.replace(b, '') for a, b in zip(df['string'], df['substring'])]

returns TypeError: replace() argument 1 must be str, not float my approach would be to replace NaN values with strings but that's a rather ugly solution. I was wondering if there was a better way to skip the row with the NaN value from being processed by the replace function.

CodePudding user response:

What if you convert b to str:

df['string1'] = [a.replace(str(b), '') for a, b in zip(df['string'], df['substring'])]

  string substring string1
0    foo        oo       f
1    moo         m      oo
2    too       NaN     too
  • Related