Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

i want to convert dataframe into list of list which contains column name in first list and data in others

i want to convert this Dataframe into list of list

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

this is the expected output

[
    ['Year', 'Sales', 'Expenses', 'Profit'],
    ['2014', 1000, 400, 200],
    ['2015', 1170, 460, 250],
    ['2016', 660, 1120, 300],
    ['2017', 1030, 540, 350]
]
like image 496
mayank chauhan Avatar asked Dec 09 '25 13:12

mayank chauhan


1 Answers

Try using:

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

Output:

[['Year', 'Sales', 'Expenses', 'Profit'], [2014, 1000, 400, 200], [2015, 1170, 460, 250], [2016, 660, 1120, 300], [2017, 1030, 540, 350]]
like image 121
U12-Forward Avatar answered Dec 12 '25 14:12

U12-Forward



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!