在python中附加数据框 [英] Appending dataframes in python

查看:44
本文介绍了在python中附加数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Append在python中使用for循环不起作用

Append is not working using for loop in python

没有for循环就可以使用,但是没有for循环就可以使用

It is working without for loop but It's not working using for loop

import os as o
import pandas as pd
j=0
ls=[]
files = o.listdir("demo")
for i in files:
  ls.append(i)
df=pd.read_csv("demo/"+ls[0])
t=len(ls)
for i in range(1,t):
  temp=pd.read_csv("demo/"+ls[i])
  df.append(temp,ignore_index = True)
print(df)

推荐答案

使用df.append效率很低,应该改为

Using df.append is highly inefficient, you should instead

dfs = [pd.read_csv("demo/"+ls[i]) for i in range(1, len(ls))]
df = pd.concat(dfs)

这篇关于在python中附加数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆