I'm new to Python and currently seeking help with the following:
How can I identify data rows for the last 10 days in CVS file with Pandas? My first column (report_date) in CSV file has data values (yyyy-mm-dd) I have hundreds of records for each day, but I need to get only last 10 days from this file, based on the date in report_date column and ideally save output to a new CSV file.
My code so far:
import pandas as pd
data = pd.read_csv("path/to/my/file/myfile.csv")
df = pd.DataFrame(report_date)
days=10
cutoff_date = df["report_date"].dt.date.iloc[-1] - pd.Timedelta(days=days)
Would someone be able to help? Thanks in advance!
Create DatetimeIndex
first with index_col
and parse_dates
parameters in read_csv
:
df = pd.read_csv("path/to/my/file/myfile.csv",
index_col=['report_date'],
parse_dates=['report_date'])
And then is possible use DataFrame.last
:
df1 = df.last('10d')
And last save to file by DataFrame.to_csv
:
df1.to_csv('new.csv')
Your solution should be changed with convert column to datetimes in read_csv
:
df = pd.read_csv("path/to/my/file/myfile.csv", parse_dates=['report_date'])
days=10
cutoff_date = df["report_date"].dt.date.iloc[-1] - pd.Timedelta(days=days)
Then compare dates by Series.dt.date
in boolean indexing
:
df1 = df[df["report_date"].dt.date > cutoff_date]
Last save to file with removed default index by DataFrame.to_csv
:
df1.to_csv('new.csv', index=False)
EDIT: I believe you need:
df = pd.DataFrame({'data': range(30)}, index= pd.date_range('2020-01-25', periods=30))
print (df)
data
2020-01-25 0
2020-01-26 1
2020-01-27 2
2020-01-28 3
2020-01-29 4
2020-01-30 5
2020-01-31 6
2020-02-01 7
2020-02-02 8
2020-02-03 9
2020-02-04 10
2020-02-05 11
2020-02-06 12
2020-02-07 13
2020-02-08 14
2020-02-09 15
2020-02-10 16
2020-02-11 17
2020-02-12 18
2020-02-13 19
2020-02-14 20
2020-02-15 21
2020-02-16 22
2020-02-17 23
2020-02-18 24
2020-02-19 25
2020-02-20 26
2020-02-21 27
2020-02-22 28
2020-02-23 29
today = pd.Timestamp('today').floor('d')
df1 = df[df.index > today].first('10d')
print (df1)
data
2020-02-11 17
2020-02-12 18
2020-02-13 19
2020-02-14 20
2020-02-15 21
2020-02-16 22
2020-02-17 23
2020-02-18 24
2020-02-19 25
2020-02-20 26
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.