[英]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.