Reputation: 979
I am trying to replace the value in my whole dataframe if part of the string match. I tried to use df.replace
df.str.contains
etc but none of them are working. Here are similar questions but they are not answering my question [sample][1]:
I have datframe like this: df
Brand 2Brand 3Brand
Audi BMW None of the above
None from list BMW Audi
None None below or above BMW
NaN Audi NaN
I just want to replace everywhere None appear.
The desired otput would be like this:
Brand 2Brand 3Brand
Audi BMW None
None BMW Audi
None None BMW
NaN Audi NaN
Upvotes: 0
Views: 81
Reputation: 863226
Use DataFrame.mask
with Series.str.contains
per columns in DataFrame.apply
, for avoid convert possible None
and NaN
values is used na=False
parameter:
df = df.mask(df.apply(lambda x: x.str.contains('None', na=False)), 'None')
print (df)
Brand 2Brand 3Brand
0 Audi BMW None
1 None BMW Audi
2 None None BMW
EDIT: If possible some numeric columns use DataFrame.select_dtypes
for get only strings columns (obviously objects ate strings) and then add to mask numeric column filled by False
by DataFrame.reindex
:
print (df)
Brand 2Brand 3Brand col
0 Audi BMW None of the above 4
1 None from list BMW Audi 7
2 None None below or above BMW 9
3 NaN Audi NaN 5
mask = (df.select_dtypes(object)
.apply(lambda x: x.str.contains('None', na=False))
.reindex(df.columns, fill_value=False, axis=1))
df = df.mask(mask, None)
print (df)
Brand 2Brand 3Brand col
0 Audi BMW None 4
1 None BMW Audi 7
2 None None BMW 9
3 NaN Audi NaN 5
Upvotes: 2
Reputation: 34086
In [1732]: df.mask(df.applymap(lambda x: 'None' in x), None)
Out[1732]:
Brand 2Brand 3Brand
0 Audi BMW None
1 None BMW Audi
2 None None BMW
Upvotes: 1