将元组列表转换为熊猫数据框的单列?

Nir*_*oda 3 python dataframe python-3.x pandas

我有一个像这样的元组列表:

list_t = [(1,2),(1,7),(1,8),(2,6),(5,8)]
Run Code Online (Sandbox Code Playgroud)

我想制作数据框,但只有一列:

在此处输入图片说明

目前使用这个

df_com = pd.DataFrame(list_t,columns=["so","des"])
Run Code Online (Sandbox Code Playgroud)

但后来我必须再次加入他们,因此增加了运营成本。

感谢您的帮助

jez*_*ael 6

将元组列表转换为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
Run Code Online (Sandbox Code Playgroud)

对于 DataFrame 添加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)
Run Code Online (Sandbox Code Playgroud)