:D
I have a dataframe like that, which each 5 mins categorical information comes to be appened in it.
The index is the same date (ìndex_date) and the categorical data is the column Fruits. The column Number counts how much of each fruit it recieves each 5 minutes, always increasing their valor and the column Diff_number is the diference with the last categorical data incoming.
The col Diff_number is made with:
df['Diff_number'] = df.groupby(['Fruit'])['Number'].diff().fillna(0)
| index_date | Fruits | Number | Diff_number |
|---|---|---|---|
| 16:10:16.000 | Apple | 1 | 0 |
| 16:10:16.000 | Grapes | 3 | 0 |
| 16:10:16.000 | Orange | 4 | 0 |
| 16:15:16.000 | Apple | 5 | 4 |
| 16:15:16.000 | Grapes | 8 | 5 |
| 16:15:16.000 | Orange | 10 | 6 |
| 16:20:16.000 | Apple | 10 | 5 |
| 16:20:16.000 | Grapes | 8 | 0 |
| 16:20:16.000 | Orange | 1 | -9 |
For example. At 16:10:16.000 it gets 1 apple with 0 difference and 5 mins after it gets in total 5 apples with 4 of difference.
All is good here but my question is:
Is there any way to eliminate that -9 and always takes the number in the column Number?
What I expect is this:
| index_date | Fruits | Number | Diff_number |
|---|---|---|---|
| 16:10:16.000 | Apple | 1 | 0 |
| 16:10:16.000 | Grapes | 3 | 0 |
| 16:10:16.000 | Orange | 4 | 0 |
| 16:15:16.000 | Apple | 5 | 4 |
| 16:15:16.000 | Grapes | 8 | 5 |
| 16:15:16.000 | Orange | 10 | 6 |
| 16:20:16.000 | Apple | 10 | 5 |
| 16:20:16.000 | Grapes | 8 | 0 |
| 16:20:16.000 | Orange | 1 | 1 |
When the col Number for some reason stops to count incrementally, Diff_number col takes the value of Number only in that row.
Actualization I tried to do that:
if df.iloc[index]['Diff_number'] < 0:
df.iloc[index]["Diff_numver"] = df.iloc[index]["Number"]
But it doesn't work :c
CodePudding user response:
You could do something like:
if df.iloc[row]['Diff_number'] < 0:
df.at[row, 'Diff_number'] = df.iloc[row]['Number']
