我想将数据框转换为列表列表,其中包含第一个列表中的列名和其他数据

我想将此数据框转换为列表列表

Year  Sales  Expenses  Profit
2014  1000   400       200
2015  1170   460       250
2016  660    1120      300
2017  1030   540       350

这是预期的输出

[
    ['Year','Sales','Expenses','Profit'],['2014',1000,400,200],['2015',1170,460,250],['2016',660,1120,300],['2017',1030,540,350]
]
kukududushen 回答:我想将数据框转换为列表列表,其中包含第一个列表中的列名和其他数据

尝试使用:

print([df.columns.tolist()] + df.values.tolist())

输出:

[['Year','Sales','Expenses','Profit'],[2014,1000,400,200],[2015,1170,460,250],[2016,660,1120,300],[2017,1030,540,350]]
本文链接:https://www.f2er.com/3161965.html

大家都在问