[英]How to replace a part of column value with values from another two columns based on a condition in pandas
[英]How to replace the value of a column base on the condition of another 2 columns?
如果“活動”列中的值等於0,或者“類別”中的值等於NaN,則“位置”中該特定行中的值應= 0。
之前;
active position category
1 1 NaN
1 2 NaN
1 1 a
0 1 a
0 1 a
1 2 a
1 1 b
0 1 b
1 2 b
后:
active position category
1 0 NaN
1 0 NaN
1 1 a
0 0 a
0 0 a
1 2 a
1 1 b
0 0 b
1 2 b
一種可能的解決方案,IIUC。 將滿足條件的所有行設置為0:
In [355]: df.loc[(df.active == 0) | (df.category.isnull()), 'position'] = 0; df
Out[355]:
active position category
0 1 0 NaN
1 1 0 NaN
2 1 1 a
3 0 0 a
4 0 0 a
5 1 2 a
6 1 1 b
7 0 0 b
8 1 2 b
所有其他行保持不變。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.