【问题标题】:Append multiple csv into a dataframe using for loop [duplicate]使用for循环将多个csv附加到数据框中[重复]
【发布时间】:2019-11-01 07:25:37
【问题描述】:

我需要遍历文件夹中的所有文件以打开所有 csv 文件。在我得到一个列表中的所有文件目录之后:我需要阅读它们并将它们附加到具有相同列名的数据框中。

a = [] #empty list
for root, dirs, files in os.walk('C:\\Users\\thaiq\\Week5'):
    for file in files:
        if file.endswith('.csv') and 'Rentals' in file:
            a.append(str(os.path.join(root,file)))

ridedata = pd.DataFrame([]) #empty dataframe

for rides in a:
    file = pd.read_csv(rides,header=0) #open each file in list a
    ridedata.append(file) 

ridedata返回空,请帮助:(

【问题讨论】:

  • 与python list.append不同,df.append实际上返回的是需要赋值的结果。但除此之外,您应该改用 pd.concat。

标签: python python-3.x


【解决方案1】:
ridedata = pd.concat([ridedata, file])

【讨论】:

    猜你喜欢
    • 2018-07-20
    • 2017-05-19
    • 2020-06-02
    • 2021-01-16
    • 2021-03-15
    • 2019-07-12
    • 2017-03-22
    • 2019-04-13
    • 2018-06-17
    相关资源
    最近更新 更多