[英]Python pandas read multiple csv file and turn into dataframe
I tried to read multiple.csv files that have the same format from different folders.我试图从不同的文件夹中读取多个具有相同格式的.csv 文件。 It turns out to be a list using.append and I tried to turn it into dataframe using.concat.
原来是一个 using.append 的列表,我试图将它变成 dataframe using.concat。 But it didn't let me to do so.
但它不允许我这样做。 I also tried.os to read the data.
我也试过.os 来读取数据。 It wouldn't work.
它行不通。 Any suggestions?
有什么建议么?
test = []
train = []
for f in testdata:
test.append(pd.read_csv(f, skiprows = 5, sep = ',', names =
['time','avg_rss12','var_rss12','avg_rss13','var_rss13','avg_rss23','var_rss23']))
for f in traindata:
train.append(pd.read_csv(f, skiprows = 5, sep = ',', names =
['time','avg_rss12','var_rss12','avg_rss13','var_rss13','avg_rss23','var_rss23']))
You mention attempting pandas concat
, but don't specify how it failed.您提到尝试 pandas
concat
,但没有说明它是如何失败的。 It is the way you would want to go.这是您想要 go 的方式。 Using just your test loop from the question:
仅使用问题中的测试循环:
import pandas as pd
frames = []
for f in testdata:
df = pd.read_csv(f, skiprows=5, sep=',', names=['time','avg_rss12','var_rss12','avg_rss13','var_rss13','avg_rss23','var_rss23'])
frames.append(df)
combined = pd.concat(frames)
What gets passed into concat()
needs to be a list of DataFrame instances, which it will combine into a single frame.传递给
concat()
的内容必须是 DataFrame 实例的列表,它将组合成一个帧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.