Take the following sample dataframe:
df = pd.DataFrame([['de', None, None],
['de ditos', 2, 3],
[4, None, None],
[None, None, 9],
['de', 4, 6]])
which looks like
0 1 2
0 de NaN NaN
1 de ditos 2.0 3.0
2 4 NaN NaN
3 None NaN 9.0
4 de 4.0 6.0
I Want to replace all the values in the column 0 where the cell value is 'de' with None, so that the dataframe ends like this:
0 1 2
0 None NaN NaN
1 de ditos 2.0 3.0
2 4 NaN NaN
3 None NaN 9.0
4 None 4.0 6.0
I have tried:
df[0].where(df[0] == 'de') = None
which returns SyntaxError: can't assign to function call
I also tried:
def erasedes(x):
if x == 'de':
return None
else: pass
df[0] = df[0].apply(lambda x: erasedes(x))
But this replaces every value with None