'Add a new column to Pandas Dataframe based on values from other column
This is my first time posting here. I tried almost everything but could not find a solution to it. Please help!
I have a python pandas dataframe which has these columns - ID, Step, X, Y. Each ID has a number of steps. I want to add a new column (new_id) to it which takes integer values starting from "1". And provides the same value for each ID if it contains same values for "X" & "Y" for all the steps using a for loop. Otherwise, add 1 to the previous new_ID value
DataFrame (df)
ID Step X Y
1001 0 100 200
1001 1 200 300
1001 2 100 250
1001 3 150 200
1002 0 150 200
1002 1 200 250
1002 2 250 300
1002 3 300 150
1003 0 100 200
1003 1 200 300
1003 2 100 250
1003 3 150 200
1004 0 150 200
1004 1 200 250
1004 2 250 300
1004 3 300 150
1005 0 125 220
1005 1 200 250
1005 2 250 300
1005 3 300 150
Newly Created DataFrame (df)
ID Step X Y new_id
1001 0 100 200 1
1001 1 200 300 1
1001 2 100 250 1
1001 3 150 200 1
1002 0 150 200 2
1002 1 200 250 2
1002 2 250 300 2
1002 3 300 150 2
1003 0 100 200 1
1003 1 200 300 1
1003 2 100 250 1
1003 3 150 200 1
1004 0 150 200 2
1004 1 200 250 2
1004 2 250 300 2
1004 3 300 150 2
1005 0 125 220 3
1005 1 200 250 3
1005 2 250 300 3
1005 3 300 150 3
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|