[英]Creating multiple csv files from existing csv file python pandas
I'm trying to take a large csv file and write a csv file for the sort of two columns.我正在尝试获取一个大的 csv 文件并为两列的排序编写一个 csv 文件。 I was able to get the two individual unique values from the file to be able to know which csv files need to be created.
我能够从文件中获取两个单独的唯一值,以便能够知道需要创建哪些 csv 文件。
Ex Data:防爆数据:
1,224939.203,1243008.651,1326.774,F,C-GRAD-FILL,09/22/18 07:24:34,
1,225994.242,1243021.426,1301.772,BS,C-GRAD-FILL,09/24/18 08:24:18,
451,225530.332,1243016.186,1316.173,GRD,C-TOE,10/02/18 11:49:13,
452,225522.429,1242996.017,1319.168,GRD,C-TOE KEY,10/02/18 11:49:46,
I would like to create a csv file "C-GRAD-FILL 09-22-18.csv" with all of the data that matches the two values.我想创建一个 csv 文件“C-GRAD-FILL 09-22-18.csv”,其中包含与两个值匹配的所有数据。 I cannot decide how to iterate through the data for both values.
我无法决定如何遍历两个值的数据。
def readData(fileName):
df = pd.read_csv(fileName,index_col=False, names+['Number','Northing','Easting','Elevation','Description','Layer','Date'],parse_dates=['Date'] )
##Layers here!!!
layers = df['Layer'].unique()
##Dates here!!! AS DATETIME OBJECTS!!!!
dates = df['Date'].map(lambda t: t.date()).unique()
##Sorted in order
sortedList = df.sort_values(by=['Layer','Date'])
You can use a GroupBy
object.您可以使用
GroupBy
对象。 First ensure your date is in the correct string format:首先确保您的日期采用正确的字符串格式:
df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%m-%d-%y')
To output all files, iterate a GroupBy
object:要输出所有文件,请迭代
GroupBy
对象:
for (layer, date), group in df.groupby(['Layer', 'Date']):
group.to_csv(f'{layer} {date}.csv', index=False)
Or, for one specific combination:或者,对于一种特定的组合:
layer = 'C-GRAD-FILL'
date = '09-22-18'
g = df.groupby(['Layer', 'Date'])
g.get_group((layer, date)).to_csv(f'{layer} {date}.csv', index=False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.