简体   繁体   English

如何使用python将存在于多个数据框中的特定列写入列表?

[英]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: 所以我需要两件事:

  1. 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) '。

  2. 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.

相关问题 使用python检查另一个数据帧上存在的一个数据帧中的多行 - Check multiple rows in one dataframe present on another dataframe using python 如何使用python检查CSV文件的特定列中是否存在列表中的字符串? - How to check if a string in a list is present in a particular column in a CSV file using python? 如何从熊猫数据框中的特定列编写多个 Excel 表? - How to write multiple excel sheets from a particular column in a pandas dataframe? 如何在 python dataframe 的列上编写和循环列表? - How to write and loop a list on a column in python dataframe? 如何根据Python中列的行中列表中的值过滤数据帧? - How to filter a dataframe based on the values present in the list in the rows of a column in Python? 如何检查Python中的列表中是否存在DataFrame字符串列的第一个单词? - How to check if first word of a DataFrame string column is present in a List in Python? 如何根据dataframe python中列中的列表值进行分组 - How to group by according to the values of a list present in a column in dataframe python 如何检查 dataframe pandas 中是否不存在列列表 - how to check if a list of column in not present in a dataframe pandas 编写一个 python function ,它采用一个列表和 dataframe 的一列并基于该列表添加一个新列 - write a python function that takes a list and one column of a dataframe and adds a new column based on that list 如何在Python DataFrame中将一列乘以其他几列 - How to multiply one column to few other multiple column in Python DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM