简体   繁体   English

如何修复 TypeError:无法连接类型为 &#39; 的对象<class 'pandas.io.parsers.TextFileReader'> &#39;; 只有 Series 和 DataFrame 对象有效吗?

[英]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参数,则:

  • it returns a TextFileReader object,它返回一个TextFileReader对象,
  • which can be used, eg in a loop, to read and process consecutive chunks.它可以用于,例如在循环中,读取和处理连续的块。

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:或者,您可能想要:

  • read only initial 20000 rows from each source file,从每个源文件中读取最初的 20000 行
  • concatenate them into a new DataFrame?将它们连接成一个新的 DataFrame?

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.

相关问题 pandas 中的 pd.concat 给出 TypeError: cannot concatenate object of type '<class 'str'> '; 只有 Series 和 DataFrame 对象有效</class> - pd.concat in pandas is giving a TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid 类型错误:无法连接类型为 &#39; 的对象<class 'yfinance.ticker.Options'> &#39;; 只有 Series 和 DataFrame 对象有效 - TypeError: cannot concatenate object of type '<class 'yfinance.ticker.Options'>'; only Series and DataFrame objs are valid 类型错误:无法连接类型为 &#39; 的对象<class 'list'> &#39;; 只有 Series 和 DataFrame 对象有效 - TypeError: cannot concatenate object of type '<class 'list'>'; only Series and DataFrame objs are valid (1)TypeError: 不能连接类型为'的object<class 'collections.ordereddict'> '; 只有系列和 DataFrame 对象有效 (2)</class> - (1)TypeError: cannot concatenate object of type '<class 'collections.OrderedDict'>'; only Series and DataFrame objs are valid (2) 类型错误:无法连接类型为 &#39; 的对象<class 'str'> &#39;; 只有 Series 和 DataFrame 对象有效 - TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid Write output of pandas.io.parsers.TextFileReader to pandas.DataFrame - Write output of pandas.io.parsers.TextFileReader to pandas.DataFrame 不能连接 ' 类型的 object<class 'numpy.ndarray'> '; 只有 Series 和 DataFrame 对象有效</class> - cannot concatenate object of type '<class 'numpy.ndarray'>'; only Series and DataFrame objs are valid 无法连接“类型”的 object<class 'float'> ";只有 pd.Series、pd.DataFrame 和 pd.Panel(已弃用)obj 有效</class> - cannot concatenate object of type "<class 'float'>"; only pd.Series, pd.DataFrame, and pd.Panel (deprecated) objs are valid 无法连接“类型”的 object<class 'numpy.ndarray'> ”; 只有 pd.Series、pd.DataFrame 和 pd.Panel(已弃用)对象有效</class> - cannot concatenate object of type “<class 'numpy.ndarray'>”; only pd.Series, pd.DataFrame, and pd.Panel (deprecated) objs are valid 使用 pandas Python (pandas.io.parsers.TextFileReader) 从文件中读取数据时出现问题 - Problem reading a data from a file with pandas Python (pandas.io.parsers.TextFileReader)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM