比较pandas Df中的每个行值元素,并根据比较输入一个字符串

2024-03-29 13:59:50 发布

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

我是新来处理熊猫Df的。 我想比较每行的每个列元素。在

要求: 如果1行的列中的所有元素都为零,则在新列中输入“More False”,该列中填充了与其索引相对应的零。在

请参见下面的Df以了解清楚

My Data Frame:

       Time     Brake      Speed         Strgangle   index   Target
0     1678.39  0.000000   0.000000        0.000000  167739      0
1     1678.40  15.00000   0.000000        0.000000  167740      0
2     1678.41  0.000000   8.000000        0.000000  167741      0
3     1678.42  0.000000   0.000000        2.000000  167742      0
4     1678.43  5.000000   20.10000        0.000000  167743      0
5     1678.44  0.150000   0.000000        -1.16500  167744      0
6     1678.45  0.000000   20.10           2.000000  167742      0
7     1678.47  0.150000   25.00000        -1.16500  167744      0


My Requirement :

1. If Brake = 0, Speed =0, Strg angle=0 
--> Input a str in corresponding Target index as 'More False'
2. If Brake = Value, Speed = Value, Strg angle=Value 
--> Input a str in corresponding Target index as 'More True'
3. As above conditions i should input the string in Target column based on my requirement

一。在

实际所需测向:

^{pr2}$

我尝试过使用If循环在Target列中输入所需的字符串,但收到的setingwithcopy警告。在

我相信会有一些简单的方法来解决上述问题。在


Tags: infalse元素targetdfinputindexif
1条回答
网友
1楼 · 发布于 2024-03-29 13:59:50

由于您只有4种可能性,请找出列中非零值的数量,然后映射结果:

d = {0: 'MoreFalse', 1: 'False', 2: 'True', 3: 'MoreTrue'}
df['Target'] = df[['Brake', 'Speed', 'Strgangle']].ne(0).sum(1).map(d)

输出:

^{pr2}$

相关问题 更多 >