简体   繁体   中英

save pandas df into several different CSV files

This is some code that will generate some random time series data. Ultimately I am trying to save each days, data into separate CSV files...

import pandas as pd 
import numpy as np 
from numpy.random import randint 

np.random.seed(10)  # added for reproductibility                                                                                                                                                                 

rng = pd.date_range('10/9/2018 00:00', periods=1000, freq='1H') 
df = pd.DataFrame({'Random_Number':randint(1, 100, 1000)}, index=rng)

I can print each days data with this:

for idx, days in df.groupby(df.index.date):
    print(days)

But how could I encorporate savings individual CSV files into a directory csv each file named with the first time stamp entry of month & day? (Code below does not work)

for idx, days in df.groupby(df.index.date):
    for day in days:
        df2 = pd.DataFrame(list(day))
        month_num = df.index.month[0]
        day_num = df.index.day[0]

        df2.to_csv('/csv/' + f'{month_num}' + '_' + f'{day_num}' + '.csv')

You could iterate over all available days, filter your dataframe and then save.

# iterate over all available days 
for date in set(df.index.date):  

    # filter your dataframe    
    filtered_df = df.loc[df.index.date == date].copy()

    # save it
    filename = date.strftime('%m_%d')  # filename represented as 'month_day'
    filtered_df.to_csv(f"./csv/{filename}.csv")  

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