我有一个数据帧
Col1 Col2 Col3 Col4 Col5
A123 13500 2/03/19 0 NaN
B123 2000 3/04/19 0 Distinct
C123 500 8/09/19 1 Match
D123 100 11/01/19 1 NaN
E123 1350 2/03/19 2 NaN
F123 2000 3/04/19 2 Match
G123 500 8/09/19 3 Distinct
H123 100 11/01/19 3 NaN
我想在基于Col4
的行中循环,并相应地更新Col5(NaN)
行。你知道吗
也就是说,当我选择Col4
为0的行时,我希望基于其他行-列值更新Col5
Output:
Col1 Col2 Col3 Col4 Col5
A123 13500 2/03/19 0 **Distinct**
B123 2000 3/04/19 0 Distinct
C123 500 8/09/19 1 Match
D123 100 11/01/19 1 **Match**
E123 1350 2/03/19 2 **Match**
F123 2000 3/04/19 2 Match
G123 500 8/09/19 3 Distinct
H123 100 11/01/19 3 **Distinct**
根据您的逻辑,似乎您希望将
Col4
中的0,3值映射到Col5
中的“Distinct”,将1,2值映射到“Match”。您只想更新NaN
中的Col5
值。你知道吗尝试:
您现在可以得到:
如果您改变了对逻辑或替换值的想法,那么以后很容易更改映射。你知道吗
好吧,我假设两件事:
1)第4列中每个数字只有两个条目
2)Col4中具有相同编号的两个条目相邻放置(实际上这并不重要,如果不是这样,您可以始终按Col4对数据帧进行排序,您将有这个case)
代码如下:
我在这里做三步。你知道吗
1)用字符串替换所有nan,这样在使用if时就不会出现任何数据类型比较问题。你知道吗
2)按升序循环。如果第5列中的值为“None”,则替换为“txt”中的值。否则,“txt”变量将与Col5中的值一起存储。你知道吗
3)同一回路的顺序相反。你知道吗
我希望这能解决你的问题。你知道吗
我想你要的是功能np.哪里. 我假设您希望在
Col4 = 0
时将值'Distinct'赋值给Col5
,在Col4 = 1
时将值'Match'。那么您的代码将是:当然,您可以根据您需要的任何条件语句修改代码
相关问题 更多 >
编程相关推荐