类型错误:无法连接“<class 'list'>”类型的对象;仅 Series 和 DataFrame 对象有效

vp_*_*050 5 python types numpy dataframe pandas

我有一个名为 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
Run Code Online (Sandbox Code Playgroud)

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

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

上面的代码抛出错误:

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

type(s1)
list
Run Code Online (Sandbox Code Playgroud)

我使用 pandas 中的 pd.concat 中建议的解决方案给出了 TypeError:cannot concatenate object of type '<class 'str'>'; 仅 Series 和 DataFrame 对象有效;但是,出现上述错误。

小智 6

s1已经是一个列表了。使用带有 DataFrames 的列表的列表执行您所做的操作,即 pd.concat,这是 pandas 不允许的。你应该这样做:

s2=pd.concat(s1)
Run Code Online (Sandbox Code Playgroud)