I would like to know how to covert multiple series objects in list into a dataframe.
import pandas as pd
data = {
"firstname": ["Sally", "Mary", "John"],
"age": [50, 40, 30],
"lastname" : ["a1", "b1", "c1"],
}
df = pd.DataFrame(data)
tmp_list = list()
for idx, row in df.iterrows():
tmp_list.append(row)
My tmp_list
looks like this.
[firstname Sally
age 50
lastname a1
Name: 0, dtype: object,
firstname Mary
age 40
lastname b1
Name: 1, dtype: object,
firstname John
age 30
lastname c1
Name: 2, dtype: object]
How could I convert the multiple series objects in tmp_list
into a dataframe like df
?
df
firstname age lastname
0 Sally 50 a1
1 Mary 40 b1
2 John 30 c1
CodePudding user response:
Use concat then use transpose:
out=pd.concat(tmp_list,axis=1).T
'''
firstname age lastname
0 Sally 50 a1
1 Mary 40 b1
2 John 30 c1
'''