如何根据其他某些列的条件用另一列的值填充一列? [英] how to fill a column with the value of another column based on a condition on some other columns?
本文介绍了如何根据其他某些列的条件用另一列的值填充一列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我们在熊猫中有一个数据框,如下所示:
Suppose that we have a dataframe in pandas as follows:
col1 | col2 | col3 | col4
22 | Nan | 23 | 56
12 | 54 | 22 | 36
48 | Nan | 2 | 45
76 | 32 | 13 | 6
23 | Nan | 43 | 8
67 | 54 | 56 | 64
16 | 32 | 32 | 6
3 | 54 | 64 | 8
67 | 4 | 23 | 64
我要替换 col4
如果 col1
和 col2
不是 col1
NaN
I want to replace the value of col4
with col1
if col4<col1
and col2
is not NaN
所以结果应该是
col1 | col2 | col3 | col4
22 | Nan | 23 | 56
12 | 54 | 22 | 36
48 | Nan | 2 | 45
76 | 32 | 13 | 76
23 | Nan | 43 | 8
67 | 54 | 56 | 67
16 | 32 | 32 | 16
3 | 54 | 64 | 8
67 | NaN | 23 | 64
我尝试了以下代码
df.loc[((df['col4'] < df['col1']) & (pd.notnull(df['col2']))), ['col4']] = df.loc['col1']
问题出在等号之后。有人知道如何解决该问题吗?
the problem is from after the equal sign. Does anyone know how to fix the problem?
推荐答案
这是一种经过验证的解决方案:
Here's a verified solution:
idx_ = df[(df['col4'] < df['col1']) & (pd.notnull(df['col2']))].index
df.loc[idx_,'col4'] = df['col1']
df
+---+------+------+------+------+
| | col1 | col2 | col3 | col4 |
+---+------+------+------+------+
| 0 | 22 | NaN | 23 | 56 |
| 1 | 12 | 54.0 | 22 | 36 |
| 2 | 48 | NaN | 2 | 45 |
| 3 | 76 | 32.0 | 13 | 76 |
| 4 | 23 | NaN | 43 | 8 |
| 5 | 67 | 54.0 | 56 | 67 |
| 6 | 16 | 32.0 | 32 | 16 |
| 7 | 3 | 54.0 | 64 | 8 |
| 8 | 67 | 4.0 | 23 | 67 |
+---+------+------+------+------+
这篇关于如何根据其他某些列的条件用另一列的值填充一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文