I have a list of 10 dataframes named d0, d1, d2,…d9. All have 3 columns and 100 rows.
JavaScript
x
12
12
1
d0.info()
2
<class 'pandas.core.frame.DataFrame'>
3
RangeIndex: 100 entries, 0 to 99
4
Data columns (total 3 columns):
5
# Column Non-Null Count Dtype
6
--- ------ -------------- -----
7
0 0 100 non-null float64
8
1 1 100 non-null float64
9
2 2 100 non-null float64
10
dtypes: float64(3)
11
memory usage: 2.5 KB
12
I want to merge all dataframes so that I can have 3 columns and 1000 rows and then convert it into an array.
JavaScript
1
3
1
s1=[d0,d1,d2,d3,d4,d5,d6,d7,d8,d9]
2
s2=pd.concat([s1])
3
The above code throws error:
JavaScript
1
5
1
TypeError: cannot concatenate object of type '<class 'list'>'; only Series and DataFrame objs are valid
2
3
type(s1)
4
list
5
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:
JavaScript
1
2
1
s2=pd.concat(s1)
2