generate time series dataframe based on a given dataframe [duplicate]
Clash Royale CLAN TAG#URR8PPP
generate time series dataframe based on a given dataframe [duplicate]
This question already has an answer here:
There is a dataframe, which includes one column of time
and another column of bill
. As can be seen from the table, there can have multiple records for a single day. The order of time
can be random
time
bill
time
time bill
2006-1-18 10.11
2006-1-18 9.02
2006-1-19 12.34
2006-1-20 6.86
2006-1-12 10.05
Based on these information, I would like to generate a time series dataframe, which has two columns Time
and total bill
Time
total bill
The time column will save the date in order, the total bill will save the sum of multiple bill records belonging to one day.
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1 Answer
1
newdf = pd.DataFrame(df.groupby('time').bill.sum())
newdf.rename(columns='time':'Time', 'bill': 'total bill', inplace = True)
newdf
output:
Time total_bill
0 2006-1-18 10.11
1 2006-1-18 9.02
2 2006-1-19 12.34
3 2006-1-20 6.86
4 2006-1-12 10.05
df.groupby('Time').bill.sum()
– Wen
Aug 13 at 2:42