简体   繁体   English

如何使用 python 使用列作为索引将多个 csv 文件连接到单个 csv 文件中

[英]How to concatenate multiple csv files into a single csv file using a column as index using python

I have to merge different csv files which contain features about a place based on place_id into one so that I can create a model to predict a rating for a particular place.我必须将不同的 csv 文件合并到一个文件中,这些文件包含基于 place_id 的地点的特征,以便我可以创建 model 来预测特定地点的评级。

I have already tried using pandas.concat and merging the files through linux terminal but I just get null values for all the other features as the place_id keeps on repeating我已经尝试使用 pandas.concat 并通过 linux 终端合并文件,但我只是得到所有其他功能的 null 值,因为 place_id 不断重复

#importing libraries
import pandas as pd
import numpy as np
import glob


#creating a single dataframe
fileList = glob.glob('chef*.csv')
fileList.append('rating_final.csv')
dfList = []
for file in fileList:
    print(file)
    df = pd.read_csv(file)
    dfList.append(df)

concatDf = pd.concat(dfList, axis=0)

I expect to get a csv file with different features according to a single place_id but what I get is a csv file in which place_id keeps on repeating with a single feature only.我希望根据单个 place_id 获得具有不同功能的 csv 文件,但我得到的是 csv 文件,其中 place_id 仅使用单个功能重复。

Try this,尝试这个,

import pandas as pd
df2 = pd.read_csv('rating_final.csv')
df2.to_csv('chef*.csv', mode='a', header=False, index=False)
test_df = pd.concat([pd.read_csv('chef*.csv'), df2], ignore_index=True, sort=True)
print(test_df)

The merged output will be available in chef*.csv file.合并后的 output 将在chef*.csv文件中可用。

暂无
暂无

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

相关问题 使用python在CSV文件中拆分单列 - Split single column in csv files using python 使用python将单列csv转换为多列csv? - converting single column csv into multiple column csv using python? 通过更改标题将.csv文件中的单个列拆分为多个列,并使用Python 2将其保存到新的.csv文件中 - Splitting a single column in a .csv file into multiple columns with changes in headings and saving it in a new .csv file using Python 2 如何在sikuli中使用python从csv文件中读取单列 - how to read single column from csv file using python in sikuli 如何使用 pandas 导入多个 csv 文件并连接成一个 DataFrame - How to import multiple csv files and concatenate into one DataFrame using pandas 如何使用python逐行连接多个CSV文件? - How do I concatenate multiple CSV files row-wise using python? 使用 python,如何读取文件夹中的所有 CSV 文件并将其内容写入新的单个 CSV 文件? - Using python, how to read all the CSV files in a folder and write the content of the same to a new single CSV file? 如何在python中使用to_csv保存多个csv文件? - How to save multiple csv files using to_csv in python? 如何将多个 CSV 文件合并到一个文件中,并使用 python 在最终 CSV 文件中创建超级模式 - how to merge multiple CSV files into one file and create super schema in final CSV file using python 在python中将多个文件循环到单个csv文件中 - Looping multiple files into a single csv file in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM