我有一个像这样的元组列表:
list_t = [(1,2),(1,7),(1,8),(2,6),(5,8)]
我想制作数据框,但只有一列:目前使用这个
df_com = pd.DataFrame(list_t,columns=["so","des"])
但后来我必须再次加入他们,因此增加了运营成本。感谢您的帮助
最佳答案
将元组列表转换为 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
对于 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)
关于python - 将元组列表转换为 Pandas 数据框的单列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66782995/