9951 explained code solutions for 126 technologies


python-pandasHow to convert dataframe to list of lists


As opposed to converting single column values to list we can also convert full dataframe to list:

import pandas as pd

data = pd.DataFrame({
  'Vendor': ['US', 'US', 'US', 'KR', 'KR'],
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Phone Price': [204, 304, None, 405, None]
})

list = data.valuesctrl + c
import pandas as pd

load Pandas module

pd.DataFrame

creates Pandas DataFrame object

.values

returns dataframe values as an multidimensional array

list

will contain final list of lists


Usage example

import pandas as pd

data = pd.DataFrame({
  'Vendor': ['US', 'US', 'US', 'KR', 'KR'],
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Phone Price': [204, 304, None, 405, None]
})

list = data.values

print(list)
output
[['US' 'ip5' 204.0]
 ['US' 'ip6' 304.0]
 ['US' 'ip8' nan]
 ['KR' 'sms' 405.0]
 ['KR' 'xi' nan]]