简体   繁体   中英

How to replace an entire cell with NaN on pandas DataFrame

I want to replace the entire cell that contains the word as circled in the picture with blanks or NaN. However when I try to replace for example '1.25 Dividend' it turned out as '1.25 NaN'. I want to return the whole cell as 'NaN'. Any idea how to work on this?

我的DataFrame

Option 1
Use a regular expression in your replace

df.replace('^.*Dividend.*$', np.nan, regex=True)

From comments

(Using regex=True ) means that it will interpret the problem as a regular expression one. You still need an appropriate pattern. The '^' says to start at the beginning of the string. '^.*' matches all characters from the beginning of the string. '$' says to end the match with the end of the string. '.*$' matches all characters up to the end of the string. Finally, '^.*Dividend.*$' matches all characters from the beginning, has 'Dividend' somewhere in the middle, then any characters after it. Then replace this whole thing with np.nan

Consider the dataframe df

df = pd.DataFrame([[1, '2 Dividend'], [3, 4], [5, '6 Dividend']])
df

   0           1
0  1  2 Dividend
1  3           4
2  5  6 Dividend

then the proposed solution yields

   0    1
0  1  NaN
1  3  4.0
2  5  NaN

Option 2
Another alternative is to use pd.DataFrame.mask in conjunction with a applymap .
If I pass a lambda to applymap that identifies if any cell has 'Dividend' in it.

df.mask(df.applymap(lambda s: 'Dividend' in s if isinstance(s, str) else False))

   0    1
0  1  NaN
1  3    4
2  5  NaN

Option 3
Similar in concept but using stack / unstack + pd.Series.str.contains

df.mask(df.stack().astype(str).str.contains('Dividend').unstack())

   0    1
0  1  NaN
1  3    4
2  5  NaN

替换所有字符串:

df.apply(lambda x: pd.to_numeric(x, errors='coerce'))

我会像这样使用applymap

df.applymap(lambda x: 'NaN' if (type(x) is str and 'Dividend' in x) else x)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM