Home > Back-end >  pandas dataframe.resample with two result(last() and sum())
pandas dataframe.resample with two result(last() and sum())

Time:04-04

I have a csv file of stock prices, and it has been read into a dataframe with the pd.read_csv method, and its format is as follows:

Time    Price   Volume
2020/1/1 9:46   7.15    1000
2020/1/1 9:46   7.15    600
2020/1/1 9:46   7.14    1000
2020/1/1 9:46   7.15    700
2020/1/1 9:46   7.15    100
2020/1/1 9:46   7.15    200
2020/1/1 9:46   7.14    400
2020/1/1 9:46   7.15    200
2020/1/1 9:46   7.15    2500
2020/1/1 9:46   7.15    1000
2020/1/1 9:46   7.15    400
2020/1/1 9:46   7.14    5400
2020/1/1 9:47   7.15    500
2020/1/1 9:47   7.15    3000
2020/1/1 9:47   7.15    1000
...

I want to use the dataframe.resample method to aggregate the 'Price' column according to the last value of each minute, and at the same time aggregate the 'Volume' column according to the sum() result? I can used the following two statements to do this separately, but how can I get these two different aggregated results at the same time? I would like the results to be saved in their respective columns as if using one of the methods alone.

df.resample('1T').last()
df.resample('1T').sum()

Thank you.

CodePudding user response:

df.resample("1T").agg({"Price" : "last", "Volume" : "sum"})
  • Related