3

I am new in handling pandas Df. I want to compare every column elements of each row.

Requirement: If all elements in the columns of 1 row is zero then input 'More False' in new column filled with zeros corresponding to its index.

See below Df for clear understanding

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

.

Actual Df required :

       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

I have tried using an If loop to input my required string in Target column, but i am receiving SettingWithcopy warning.

I am sure that there will be some easy approach for the above problem.

James Z
  • 12,209
  • 10
  • 24
  • 44
Mari
  • 698
  • 1
  • 8
  • 27
  • look at np.select – BENY Jan 23 '19 at 16:14
  • My Requirement/Conditions :(@FilipeAleixo) 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 – Mari Jan 23 '19 at 18:44

1 Answers1

3

Since you only have 4 possibilities, find the number of non-zero values across the columns, then map the result:

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

Output:

      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
ALollz
  • 57,915
  • 7
  • 66
  • 89
  • Thank you so much...It works... (But still I am getting SettingWithcopyWarning. Is there any other method to avoid such warnings !!!) – Mari Jan 23 '19 at 19:15
  • @esakkiponraj.e Please see [Question 4 at the bottom of coldspeed's answer](https://stackoverflow.com/a/53954986/4333359). The code above is not problematic,. The issue is that further up in your code you must have created a view from a slicing operation, so it will keep giving you this warning. You will need to add `.copy()` after your slicing or instead use `.loc` to slice. – ALollz Jan 23 '19 at 19:20
  • Thank you @ALollz Also, could you kindly explain the usage of .ne function here.... I am surfing about the .ne function, but i cant get the clear idea of its usage !! Thank you... – Mari Jan 23 '19 at 19:46
  • @esakkiponraj.e it's just a convenience wrapper for writing `DataFrame != 0` (which will perform the element wise comparison of each value with 0). This allows you to chain operations with fewer closing parenthesis and characters. `ne(0)` is read as "not equal to zero". There is also `.eq()` for equal to and `.lt()`, `.le()`, `.gt()`, `.ge()` for less than, less than or equal to, greater than and greater than or equal to. – ALollz Jan 23 '19 at 19:52