简体   繁体   English

将 pandas 中的多个 .csv 文件的单列行合并

[英]combine rows of single column over multiple .csv files in pandas

I have a bunch of.csv files with the same column headers and data types in the columns.我有一堆 .csv 文件,它们在列中具有相同的列标题和数据类型。

c1   c2   c3 
1    5   words
2    6   words
3    7   words
4    8   words

is there a way to combine all the text in c3 in each.csv file then combine them into one csv?有没有办法将每个.csv 文件中c3 中的所有文本组合成一个csv?

I combined them this way我这样组合它们

path = r'C:\\Users\\...\**\*.csv'

all_rec = iglob(path, recursive=True)     
dataframes = (pd.read_csv(f) for f in all_rec)
big_dataframe = pd.concat(dataframes, ignore_index=True)

i'm not sure how to combine the text rows first then bring them together.我不确定如何先组合文本行,然后再将它们组合在一起。

There are many way to do it.有很多方法可以做到这一点。 One way:单程:

path = r'C:\\Users\\...\**\*.csv'

all_rec = iglob(path, recursive=True)

# Extract only c3 column from files
dataframes = {f: pd.read_csv(f, usecols=['c3']) for f in all_rec}

# Group all dataframes then combine text rows of each dataframe
big_dataframe = pd.concat(dataframes).groupby(level=0)['c3'] \
                  .apply(lambda x: ' '.join(x.tolist())).reset_index(drop=True)

Output: Output:

>>> big_dataframe
0        words words words words
1    words2 words2 words2 words2
2    words3 words3 words3 words3
Name: c3, dtype: object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Pandas Dataframe 中将多行合并为单行 - Combine multiple rows into single row in Pandas Dataframe 在熊猫数据框中将多行合并为一行 - Combine multiple rows to a single line in pandas data frame 如何根据 pandas 中的条件将多行合并为单行 - How can combine multiple rows into single based on condition in pandas 将多个 csv 文件组合成一个 xls 工作簿 Python 3 - Combine multiple csv files into a single xls workbook Python 3 使用 python 中的特定列将多行组合成单行 - Combine multiple rows into Single row using particular column in python 使用 python 根据特定列将多行合并为单行 - Combine multiple rows into Single row based on specific column using python Pandas:将 CSV 数据从单个列重新格式化为多个新列 - Pandas: reformatting CSV data from single column into multiple new columns Python 3.x:Pandas DataFrame我们如何将多个csv文件合并为一个csv文件? - Python 3.x: Pandas DataFrame How do we combine multiple csv files into one csv file? 如何使用 Pandas 将 csv 文件的大数据按列合并到单个 csv 文件中? - How to merge big data of csv files column wise into a single csv file using Pandas? 使用 Pandas:如何基于一个公共键将多行数据组合成一行? - Using Pandas: How do I combine multiple rows of data into a single row based on a common key?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM