在for循环中加入数据框

时间:2018-12-05 03:22:54

标签: python

我的代码无法正常工作...它给了我ValueError: columns overlap but no suffix specified

import pandas as pd
import pickle 

list = ["ZILLOW2.csv", "ZILLOW3.csv", "ZILLOW4.csv", 
"ZILLOW6.csv", "ZILLOW7.csv", "ZILLOW8.csv"]
maindf = pd.DataFrame()       
for x in list:  
    df = pd.read_csv(x)    
    if x == "ZILLOW2.csv":
        maindf = pd.DataFrame(df)
    else:
        maindf = maindf.join(df)

print(maindf)

1 个答案:

答案 0 :(得分:0)

使用concat

import pandas as pd
import pickle 

list = ["ZILLOW2.csv", "ZILLOW3.csv", "ZILLOW4.csv", 
"ZILLOW6.csv", "ZILLOW7.csv", "ZILLOW8.csv"]
maindf = pd.DataFrame()       
for x in list:  
    df = pd.read_csv(x)    
    if x == "ZILLOW2.csv":
        maindf = pd.DataFrame(df)
    else:
        maindf = pd.concat([maindf, df], axis=1)
print(maindf)