【问题标题】:TypeError: cannot concatenate object of type '<class 'list'>'; only Series and DataFrame objs are validTypeError:无法连接类型为“<class 'list'>”的对象;只有 Series 和 DataFrame obj 是有效的
【发布时间】:2021-12-11 11:24:53
【问题描述】:

我有一个名为 d0、d1、d2、...d9 的 10 个数据帧的列表。都有 3 列和 100 行。

d0.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 100 entries, 0 to 99
Data columns (total 3 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   0       100 non-null    float64
 1   1       100 non-null    float64
 2   2       100 non-null    float64
dtypes: float64(3)
memory usage: 2.5 KB

我想合并所有数据框,这样我就可以拥有 3 列和 1000 行,然后将其转换为数组。

s1=[d0,d1,d2,d3,d4,d5,d6,d7,d8,d9]
s2=pd.concat([s1])

以上代码报错:

TypeError: cannot concatenate object of type '<class 'list'>'; only Series and DataFrame objs are valid

type(s1)
list

我使用了pd.concat in pandas is giving a TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid 中建议的解决方案;但是,出现上述错误。

【问题讨论】:

标签: python pandas dataframe numpy types


【解决方案1】:

s1 已经是一个列表。做你所做的称为 pd.concat 的列表带有 DataFrames 的列表,这是 pandas 不允许的。你应该这样做:

s2=pd.concat(s1)

【讨论】:

  • 抛出此错误“TypeError: cannot concatenate object of type ''; 只有 Series 和 DataFrame objs 有效”
  • s1 中的一项是字符串。你必须弄清楚你做错了什么,因为我给出的答案正确地连接了数据帧
猜你喜欢
  • 2021-10-04
  • 1970-01-01
  • 2022-01-21
  • 2020-05-17
  • 2020-11-05
  • 2020-08-17
  • 2021-10-23
  • 2021-01-15
  • 1970-01-01
相关资源
最近更新 更多