DF1
Pet
Dog-Ralph
Cat
2016-11-03 00:00:00
我有上面的数据框,我想转向2016-11-03 00:00:00'值为NaN值。由于它是唯一包含冒号的值,如何将该值转换为NaN,如下所示:
Pet
Dog-Ralph
Cat
NaN
感谢帮助!
答案 0 :(得分:2)
import pandas as pd
import numpy as np
data = [{'name': 'Pet'},{'name':'Dog-Ralph'},{'name':'Cat'},{'name':'2016-11-03 00:00:00'}]
df = pd.DataFrame.from_dict(data, orient='columns')
df
输出:
name
0 Pet
1 Dog-Ralph
2 Cat
3 2016-11-03 00:00:00
使用np.where更快地查找和替换:Read more
df['name'] = np.where(df['name'].str.contains(":"), np.nan, df['name'])
df
输出:
name
0 Pet
1 Dog-Ralph
2 Cat
3 NaN
请参阅np.where效果详情:Why is np.where faster than pd.apply
答案 1 :(得分:1)
你可以试试这个:
df
# Pet
#0 Dog-Ralph
#1 Cat
#2 2016-11-03 00:00:00
import numpy as np
df[df["Pet"].notnull() & df['Pet'].str.contains(":")] = np.nan
# replace any non-nan string with `colon` with np.nan
df
# Pet
#0 Dog-Ralph
#1 Cat
#2 NaN
或者,如果您只需要替换Pet
列:
df.loc[df["Pet"].notnull() & df["Pet"].str.contains(":"), "Pet"] = np.nan