Skip to content
Advertisement

TypeError: cannot concatenate object of type ”; only Series and DataFrame objs are valid

I have a list of 10 dataframes named d0, d1, d2,…d9. All have 3 columns and 100 rows.

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

I want to merge all dataframes so that I can have 3 columns and 1000 rows and then convert it into an array.

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

The above code throws error:

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

type(s1)
list

I used the solution suggested in pd.concat in pandas is giving a TypeError: cannot concatenate object of type ‘<class ‘str’>’; only Series and DataFrame objs are valid ; however, got the above error.

Advertisement

Answer

s1 is already a list. Doing what you did called pd.concat with a list of a list with DataFrames, which pandas doesn’t allow. You should do it like this instead:

s2=pd.concat(s1)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement