[英]How to fix TypeError: cannot concatenate object of type '<class 'pandas.io.parsers.TextFileReader'>'; only Series and DataFrame objs are valid?
I am trying to read csv files and concatenate them and output them as one csv file.我正在尝试读取 csv 文件并将它们连接起来并将它们作为一个 csv 文件输出。 I keep getting this error:
我不断收到此错误:
TypeError: cannot concatenate object of type '< class 'pandas.io.parsers.TextFileReader'>';
类型错误:无法连接类型为 '< class 'pandas.io.parsers.TextFileReader'>' 的对象; only Series and DataFrame objs are valid;
只有 Series 和 DataFrame 对象有效;
I am not sure how to fix it.我不知道如何解决它。 I am a beginner, so I would appreciate any help!
我是初学者,所以我很感激任何帮助! Thank you!
谢谢! Here is the code I wrote:
这是我写的代码:
csv.field_size_limit(sys.maxsize)
df1 = pd.read_csv('file1.csv', chunksize=20000)
df2 = pd.read_csv('file2.csv', chunksize=20000)
df3 = pd.read_csv('file3.csv', chunksize=20000)
df4 = pd.read_csv('file4.csv', chunksize=20000)
df5 = pd.read_csv('file5.csv', chunksize=20000)
df6 = pd.read_csv('file6.csv', chunksize=20000)
frames = [df1, df2, df3, df4, df5, df6]
result = pd.concat(frames, ignore_index=True, sort=False)
result.to_csv('new.csv')
If you call read_csv passing chunksize parameter, then:如果您调用read_csv传递chunksize参数,则:
An example of how to use "chunked" CSV file reading:如何使用“分块”CSV 文件读取的示例:
reader = pd.read_csv('input.csv', chunksize=20000)
for chunk in reader:
# Process the chunk (DataFrame)
Or maybe you want to:或者,您可能想要:
If this is the case, pass nrows=20000 (instead of chunksize ), while reading from each file.如果是这种情况,请在读取每个文件时传递nrows=20000 (而不是chunksize )。 Then all returned objects will be just DataFrames and you will be able to concat them.
然后,所有返回的对象将只是DataFrames,你将能够Concat的他们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.