vamsi shankar
vamsi shankar

Reputation: 111

Getiing error while converting a dataframe to a list

Below is the code, imported the data to data frame but unable to convert it into a list. Getting TypeError:'list' object is not callable

import pandas
import numpy
import random
dataframe = pandas.read_csv('data.csv')
list= ['Gender']
dataset = dataframe.drop(list, axis=1)
print(list(dataset))

Upvotes: 2

Views: 58

Answers (3)

Avi Turner
Avi Turner

Reputation: 10456

In python, you can override language keywords and data types, in your case - the list data type. Have a look:

print('type of list: {0}'.format(type(list)))
list = ['Gender']
print('type of list: {0}'.format(type(list)))

outputs:

type of list: <type 'type'>
type of list: <type 'list'>

I would suggest changing your variable name to something other than list

Upvotes: 0

jezrael
jezrael

Reputation: 863431

Problem is code variable list as variable name, better is use L.

Solution is reassign list by list = builtins.list or after rename variable restart your IDE:

import pandas as pd
import numpy as np
import random
import builtins

#reassign list
list = builtins.list

dataframe = pd.read_csv('data.csv')
L = ['Gender']
dataset = dataframe.drop(L, axis=1)
#if want columns names in list
print(list(dataset))
#if want all values of df to nested lists
print(dataset.values.tolist())

Upvotes: 3

user3483203
user3483203

Reputation: 51175

You created a variable called list, so when you try to call the list constructor, you're met with an error, since list now refers to a list, instead of a type constructor. Don't use builtin names as variable names.

You could also just use dataset.columns.tolist()

Upvotes: 3

Related Questions