我有一个数据框,我正在检查它是否Y在所有列中,否则返回N,如果行中的所有列均为 Null,则返回 Null。
di = {'col1': [None, 'Y', 'N'], 'col2': [None, 'Y', 'N'], 'col3': [None, 'N', 'N']}
df = pd.Dataframe(di)
df['test'] = pd.np.where(df[['col1', 'col2', 'col3']].eq('Y').any(1, skipna=True), 'Y', 'N')
Run Code Online (Sandbox Code Playgroud)
此返回:
col1 col2 col3 test
0 None None None N
1 Y Y N Y
2 N N N N
Run Code Online (Sandbox Code Playgroud)
我希望它能回来
col1 col2 col3 test
0 None None None None
1 Y Y N Y
2 N N N N
Run Code Online (Sandbox Code Playgroud)
您可以将另一个numpy.where包裹在里面来检查 null 条件:
df['test'] = pd.np.where(df[['col1', 'col2', 'col3']].eq('Y').any(1, skipna=True), 'Y',
pd.np.where(df[['col1', 'col2', 'col3']].isnull().all(1), None, 'N'))
df
# col1 col2 col3 test
#0 None None None None
#1 Y Y N Y
#2 N N N N
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12419 次 |
| 最近记录: |