[英]How to write one particular column present in multiple dataframe to a list using python?
I have 4 csv files in a folder, and I load them individually as dataframes in python as dataframes. 我在一个文件夹中有4个csv文件,我将它们分别作为数据帧加载到python中作为数据帧。 I process each of these dataframes, to get the unique 'file name' alone as a list and write it to a new csv file.
我处理这些数据帧中的每一个,以单独获得唯一的“文件名”作为列表并将其写入新的csv文件。
Now I want to write all the file names of all the dataframes into output file. 现在,我想将所有数据帧的所有文件名写入输出文件。
file_list=[]
for fileno in data.groupby(['date','age'])['File_No']:
file_list.append(fileno)
with open(r'D:\Data\core_data\file1.csv', "w") as csvFile:
writer = csv.writer(csvFile)
writer.writerows(file_list)
here data is one dataframe. 这里的数据是一个数据帧。 This yields me the list of files names present in this dataframe as follows:
这为我提供了此数据框中存在的文件名列表,如下所示:
[((Timestamp('2018-01-15 00:00:00'), '1', 1), 0 1011
1 1012
2 1013
3 1014...]
So I need two things: 所以我需要两件事:
I dont want the '((Timestamp('2018-01-15 00:00:00'), '1', 1)
' in the list output. 我不希望列表输出中的
'((Timestamp('2018-01-15 00:00:00'), '1', 1)
'。
The lists of all the dataframes should be written to one lists of list as : 所有数据框的列表应写入列表的一个列表中,如下所示:
[[list of file_1 file names],[list of file_2 file names],[list of file_3 file names]]
[[file_1文件名列表],[file_2文件名列表],[file_3文件名列表]
You intend to get a list of list of the file names present in your 4 csv files correct? 您打算正确列出4个csv文件中存在的文件名列表吗?
In this case why don't you loop over the CSV files and grab the expected list as follow: 在这种情况下,为什么不循环遍历CSV文件并按以下方式获取所需列表:
import pandas as pd
files = ['file1.csv', 'file2.csv', 'file3.csv', 'file4.csv']
output = []
for file in files:
temp_df = pd.read_csv(file)
output.append([x for x in list(temp_df['File_No'].unique()) if type(x) == int])
#write output to csv...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.