我想把这个Dataframe转换成list of list
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]
]
最佳答案
尝试使用:
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]]
关于python - 我想将数据框转换为列表列表,其中包含第一个列表中的列名和其他列表中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58705359/