通过排除表中另一列中的特定值来填充一列

2024-06-06 14:34:23 发布

您现在位置:Python中文网/ 问答频道 /正文

对于数据帧df,我试图用值2017-01-01填充列b,如果列a中的值为空NaNsOthers

df = pd.DataFrame({'a':['Coffee','Muffin','Donut','Others',pd.np.nan, pd.np.nan]})
        a
0  Coffee
1  Muffin
2   Donut
3  Others
4     NaN
5     NaN

预期结果如下:

        a           b
0  Coffee  2017-01-01
1  Muffin  2017-01-01
2   Donut  2017-01-01
3  Others         NaN
4     NaN         NaN
5     NaN         NaN

我尝试过的不排除NaNs

df.loc[~df['a'].isin(['nan', 'Others']), 'b'] = '2017-01-01'

        a           b
0  Coffee  2017-01-01
1  Muffin  2017-01-01
2   Donut  2017-01-01
3  Others         NaN
4     NaN  2017-01-01
5     NaN  2017-01-01

谢谢!你知道吗


Tags: 数据dataframedfnpnanlocpdcoffee
3条回答

np.nan代替nan

df.loc[~df['a'].isin([np.nan, 'Others']), 'b'] = '2017-01-01'

或者在比较之前,用Others替换缺少的值:

df.loc[~df['a'].fillna('Others').eq('Others'), 'b'] = '2017-01-01'
print (df)
        a           b
0  Coffee  2017-01-01
1  Muffin  2017-01-01
2   Donut  2017-01-01
3  Others         NaN
4     NaN         NaN
5     NaN         NaN
import pandas as pd
import numpy as np

df = pd.DataFrame({'a':['Coffee','Muffin','Donut','Others',pd.np.nan, pd.np.nan]})


df.loc[df['a'].replace('Others',np.nan).notnull(),'b'] = '2017-01-01'

print(df)

看看这个:

import numpy as np
import pandas as pd

df = pd.DataFrame({'a': ['Coffee', 'Muffin', 'Donut', 'Others', pd.np.nan, pd.np.nan]})
conditions = [
    (df['a'] == 'Others'),
    (df['a'].isnull())
]
choices = [np.nan, np.nan]
df['color'] = np.select(conditions, choices, default='2017-01-01')

print(df)

相关问题 更多 >