I have a quite simple DataFrame, which stores clock-in and -out times for a specific day.
This can happen multiple times a day, so for example there are multiple rows for day 2 of the month, the DataFrame df_03 represents:
>>> df_03.loc[df_03['Date'] == 2]
Date DOW from until ... counted time target time balance time_sum
1 2 MO 08:00 08:45 ... NaN NaN NaN 0 days 00:45:00
2 2 MO 09:43 17:22 ... 7.65 7.5 0.15 0 days 07:39:00
[2 rows x 9 columns]
What I want to do is to summarize the time_sum values of all rows per day and add the result to a new column called day_sum, but only for the last row of the result .loc.
So, the result should look like this:
>>> df_03.loc[df_03['Date'] == 2]
Date DOW from until ... target time balance time_sum day_sum
1 2 MO 08:00 08:45 ... NaN NaN 0 days 00:45:00 NaT
2 2 MO 09:43 17:22 ... 7.5 0.15 0 days 07:39:00 0 days 08:24:00
[2 rows x 9 columns]
I have tried it like this, but I always end up changing a subset copy of the original DataFrame:
>>> df_03.loc[df_03['Date'] == 2].iloc[-1] = df_03.loc[df_03['Date'] == 2]['time_sum'].sum()
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
>>> day = df_03.loc[df_03['Date'] == 2]
>>> day.iloc[-1]['day_sum'] = df_03.loc[df_03['Date'] == 2]['time_sum'].sum()
A value is trying to be set on a copy of a slice from a DataFrame