我有3个csv文件,它们都应该具有相同的日期列:
file1.csv file2.csv file3.csv
date,price1 date,price2 date,price3
2017-03-03,1900 2017-03-03,1200 2017-03-03,1220
2017-03-04,2900 2017-03-04,2200 2017-03-04,2233
2017-03-04,1300 2017-03-04,1549 2017-03-04,1520
我想加入他们并使用python获取:
file4.csv
date,price1,price2,price3
2017-03-03,1900,1200,1220
2017-03-04,2900,2200,2233
2017-03-04,1300,1549,1520
答案 0 :(得分:2)
一次可以组合两个文件。对其他文件也重复此过程。
import pandas as pd
df1 = pd.read_csv('file1.csv')
df2 = pd.read_csv('file2.csv')
df3 = pd.read_csv('file3.csv')
df12 = pd.merge(df1, df2, how='outer', on='date')
df123 = pd.merge(df12, df3, how='outer', on='date')
print(df123)