I have a list of tuple like this :
JavaScript
x
2
1
list_t = [(1,2),(1,7),(1,8),(2,6),(5,8)]
2
I want to make data frame out this but just one column:
Currently using this
JavaScript
1
2
1
df_com = pd.DataFrame(list_t,columns=["so","des"])
2
but then I have to join them again so operation cost is increased.
Thank you for your help
Advertisement
Answer
Convert list of tuples to Series
:
JavaScript
1
9
1
s = pd.Series(list_t)
2
print (s)
3
0 (1, 2)
4
1 (1, 7)
5
2 (1, 8)
6
3 (2, 6)
7
4 (5, 8)
8
dtype: object
9
For DataFrame add Series.to_frame
:
JavaScript
1
9
1
df = pd.Series(list_t).to_frame('new')
2
print (df)
3
new
4
0 (1, 2)
5
1 (1, 7)
6
2 (1, 8)
7
3 (2, 6)
8
4 (5, 8)
9