Skip to content Skip to sidebar Skip to footer

Extracting Data Belonging To A Day From A Given Range Of Dates On A Dataset

I have a data set with a date range from January 12th to August 3rd of 2018 with some values: The dimensionality of my_df DataFrame is: my_df.shape (9752, 2) Each row contains h

Solution 1:

Something like this? I renamed your original column of Date: to Timestamp. I am also assuming that the Date: Series you have is a pandas DateTime series.

my_df.columns = ['Timestamp', 'Value']
my_df['Date'] = my_df['Timestamp'].apply(lambda x: x.date())
dates = my_df['Date'].unique()
for date in dates:
    f_name = str(date) + '.csv'
    my_df[my_df['Date'] == date].to_csv(f_name)

Solution 2:

groupby

for date, d in df.groupby(pd.Grouper(key='Date', freq='D')):
  d.to_csv(f"Data_{date:%b_%d}.csv", index=False)

Notice I used an f-string which is Python 3.6+
Otherwise, use this

for date, d in df.groupby(pd.Grouper(key='Date', freq='D')):
  d.to_csv("Data_{:%b_%d}.csv".format(date), index=False)

Consider the df

df = pd.DataFrame(dict(
    Date=pd.date_range('2010-01-01', periods=10, freq='12H'),
    Value=range(10)
))

Then

for date, d in df.groupby(pd.Grouper(key='Date', freq='D')):
  d.to_csv(f"Data_{date:%b_%d}.csv", index=False)

And verify

from pathlib import Path

print(*map(Path.read_text, Path('.').glob('Data*.csv')), sep='\n')

Date,Value
2010-01-05 00:00:00,8
2010-01-05 12:00:00,9

Date,Value
2010-01-04 00:00:00,6
2010-01-04 12:00:00,7

Date,Value
2010-01-02 00:00:00,2
2010-01-02 12:00:00,3

Date,Value
2010-01-01 00:00:00,0
2010-01-01 12:00:00,1

Date,Value
2010-01-03 00:00:00,4
2010-01-03 12:00:00,5

Post a Comment for "Extracting Data Belonging To A Day From A Given Range Of Dates On A Dataset"