I want to create a new column in existing dataframe where the value in each row is conditional on some rule in adjacent column. Let me give an example. I have the following dictionary:
d1={"commence_time":["12/6/2019 14:00", "12/7/2019 17:30",
"12/7/2019 17:30","12/8/2019 19:30",
"12/9/2019 19:30","12/28/2019 15:00",
"12/28/2019 15:00","12/28/2019 15:00"],
"value1":["5","5","5","5","5","5","5","5"]}
and I create the following DataFrame:
df1=pd.DataFrame(data=d1)
which results in:
commence_time value1
0 12/6/2019 14:00 5
1 12/7/2019 17:30 5
2 12/7/2019 17:30 5
3 12/8/2019 19:30 5
4 12/9/2019 19:30 5
5 12/28/2019 15:00 5
6 12/28/2019 15:00 5
7 12/28/2019 15:00 5
I want to create another column called value2 where the following rule applies: If commence time is followed by different commence time in the next row, value2 in the first row should be equal to value1. If commence time is exactly the same as in the following row (or potentially even the subsequent row) then numbers in value2 are value1 summed by 5. Expected result is:
commence_time value1 value2
0 12/6/2019 14:00 5 5
1 12/7/2019 17:30 5 10
2 12/7/2019 17:30 5 10
3 12/8/2019 19:30 5 5
4 12/9/2019 19:30 5 5
5 12/28/2019 15:00 5 10
6 12/28/2019 15:00 5 10
7 12/28/2019 15:00 5 10
Don't really know how to approach this problem so I didn't really try anything. Suggestions appreciated.
Aucun commentaire:
Enregistrer un commentaire