Reputation: 3514
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')
Upvotes: 2
Views: 174
Reputation: 3483
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")
Upvotes: 3