【问题标题】:Compare every row value elements in pandas Df and input a string based on comparison比较 pandas Df 中的每一行值元素并根据比较输入一个字符串
【发布时间】:2019-01-23 16:01:02
【问题描述】:

我是处理熊猫 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

.

需要的实际 Df:

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

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

我相信对于上述问题会有一些简单的方法。

【问题讨论】:

  • 查看 np.select
  • 我的要求/条件 :(@FilipeAleixo) 1. 如果 Brake = 0, Speed =0, Strg angle=0 --> 在对应的 Target index 中输入 str 为 'More False' 2. 如果Brake = Value, Speed = Value, Strg angle=Value --> 在相应的目标索引中输入一个 str 为'More True' 3. 如上所述,我应该根据我的要求在目标列中输入字符串

标签: python python-3.x pandas dataframe warnings


【解决方案1】:

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

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

输出:

      Time  Brake  Speed  Strgangle   index     Target
0  1678.39   0.00    0.0      0.000  167739  MoreFalse
1  1678.40  15.00    0.0      0.000  167740      False
2  1678.41   0.00    8.0      0.000  167741      False
3  1678.42   0.00    0.0      2.000  167742      False
4  1678.43   5.00   20.1      0.000  167743       True
5  1678.44   0.15    0.0     -1.165  167744       True
6  1678.45   0.00   20.1      2.000  167742       True
7  1678.47   0.15   25.0     -1.165  167744   MoreTrue

【讨论】:

  • 非常感谢...它有效...(但我仍然收到 SettingWithcopyWarning。有没有其他方法可以避免此类警告!!!)
  • @esakkiponraj.e 请参阅Question 4 at the bottom of coldspeed's answer。上面的代码没有问题,。问题是,在您的代码中,您必须已经从切片操作创建了一个视图,所以它会不断地给您这个警告。您需要在切片后添加.copy(),或者使用.loc 进行切片。
  • 谢谢@ALollz 另外,您能否在这里解释一下.ne 函数的用法....我正在浏览.ne 函数,但我无法清楚地了解它的用法!谢谢...
  • @esakkiponraj.e 它只是一个用于编写DataFrame != 0 的便利包装器(它将执行每个值与0 的元素明智比较)。这允许您使用更少的右括号和字符来链接操作。 ne(0) 读作“不等于零”。还有.eq()代表等于和.lt().le().gt().ge()代表小于、小于或等于、大于和大于或等于。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多