Skip to content
Advertisement

convert list of tuples into single column of pandas dataframe?

I have a list of tuple like this :

list_t = [(1,2),(1,7),(1,8),(2,6),(5,8)]

I want to make data frame out this but just one column:

enter image description here

Currently using this

df_com = pd.DataFrame(list_t,columns=["so","des"])

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:

s = pd.Series(list_t)
print (s)
0    (1, 2)
1    (1, 7)
2    (1, 8)
3    (2, 6)
4    (5, 8)
dtype: object

For DataFrame add Series.to_frame:

df = pd.Series(list_t).to_frame('new')
print (df)
      new
0  (1, 2)
1  (1, 7)
2  (1, 8)
3  (2, 6)
4  (5, 8)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement