Issue
I have a summary dataframe. I want to extract quarterly data and export it to the quarterly folders created already.
My code:
ad = pd.DataFrame({"sensor_value":[10,20]},index=['2019-01-01 05:00:00','2019-06-01 05:00:00'])
ad =
sensor_value
2019-01-01 05:00:00 10
2019-06-01 05:00:00 20
ad.index = pd.to_datetime(ad.index,format = '%Y-%m-%d %H:%M:%S')
# create quarter column
ad['quarter'] = ad.index.to_period('Q')
ad =
sensor_value quarter
2019-01-01 05:00:00 10 2019Q1
2019-06-01 05:00:00 20 2019Q2
# quarters list
qt_list = ad['quarter'].unique()
# extract data for quarter and store it in the corresponding folder that already exist
fold_location = 'C\\Data\\'
for i in qt_list:
auxdf = ad[ad['quarter']=='%s'%(i)]
save_loc = fold_location+'\\'+str(i)
auxdf.to_csv(save_loc+'\\'+'Sensor_1minData_%s.csv'%(i))
Is there a better way of doing it? Thanks
Solution
You can use groupby
with something like:
for quarter, df in ad.groupby('quarter'):
df.to_csv(f"C\\Data\\{quarter}\\Sensor_1minData_{quarter}.csv")
Answered By - Robin Nicole
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.