It's been 3 days since I touched Python...
There are csv time series data recorded every minute, such as:
Column 1: Date (mm/dd/yy)
Second row: Time (hh:mm:ss)
Columns 3 to 12 (each data)
12/27/1808:32:14960.650 33.10.1376 1.9475.8 307.52.77 11.080.1368
12/27/1808:33:14960.550 35.60.1368 1.9475.8 15.40.251.510.1376
12/27/1808:34:14960.650 36.90.1368 1.91375.5 355.2 1.51 4.530.1376
·
·
·
01/21/1908:55:14958.950-888.90.02140.301 57.3 308.9 2.526.30.0228
01/21/1908:56:14959.150-888.90.02140.16357.7 205 2.01 5.540.0228
01/21/1908:57:14959.150-888.90.02140.163 57.7282.2 2.014.780.0228
The data period is from December 27th to January 21st, but I would like to set up a program that extracts data every day and outputs it to csv.
For example, the image below is monthly, but ideally it should be printed day by day in this way.
I tried programming using pandas, but it doesn't work well.
I'm sorry for the poor explanation, but I'd appreciate it if you could let me know...
Thank you for your cooperation.
To complement the following conditions:
Avoid dealing with abnormal data and errors.
import pandas as pd
import datetime
infilename = 'data.csv'
df=pd.read_csv(infilename, header=None, sep='\t', names=list('ABCDEFGHIJKL')))
date_list = list(df['A'].unique())
for date_strindate_list:
outfilename = datetime.datetime.strptime(date_str, '%m / %d / %y').strftime('%Y - %m - %d') + '.csv'
ds = df [df['A'].isin([date_str])]
ds.to_csv(outfilename, header=False, index=False, sep='\t')
The multi-post answer is more concise.However, our output file name looks good.
If you combine them, they will be as follows:
import pandas as pd
import datetime
infilename = 'data.csv'
df=pd.read_csv(infilename, header=None, sep='\t')
for date_str, daily_data indf.groupby(0):
outfilename = datetime.datetime.strptime(date_str, '%m / %d / %y').strftime('%Y - %m - %d') + '.csv'
daily_data.to_csv(outfilename, header=False, index=False, sep='\t')
Other articles around here will explain in detail how to use csv when reading and writing.
Learn more How to use read_csv and read_table functions in Pandas
How to export to a CSV file using Pandas' to_csv function
This is an article on how to use groupby on the same site.
How to group elements in Pandas into groups and process them
© 2024 OneMinuteCode. All rights reserved.