简体   繁体   中英

How to convert daily data into weekly or monthly in python with categorical and numerical column?

I have a daily dataset that has a categorical and numerical column. So, I want to change the daily dataset to the monthly dataset. How can I do that using python? For example, if I have a dataset similar to the picture below how can I bring it in per month having a categorical value and sum for a numerical column. It was easy to sum for the numerical columns but having categorical makes it difficult. The categorical value is most likely to be the same throughout the daily data. 在此处输入图像描述

import pandas as pd
from statistics import mode

df1_ohlc = df.set_index('Date').groupby('Name').resample('M').agg({'Open':'first','High':'max','Low':'min','Close','last':'Volume': 'sum'}).reset_index()

df2_mode = df.set_index('Date').groupby('Name').resample('M').agg({'Open':mode,'High':mode,'Low':mode,'Close',mode:'Volume': mode}).reset_index()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM