9951 explained code solutions for 126 technologies


python-pandasHow to convert dataframe to list


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]
})

values = data.values
list = values.flatten()ctrl + c
import pandas as pd

load Pandas module

pd.DataFrame

creates Pandas DataFrame object

.values

returns dataframe values as an multidimensional array

.flatten()

flatten given multidimensional array to single dimension


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]
})

values = data.values
list = values.flatten()

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