2

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

3 Answers 3

3

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()

Sign up to request clarification or add additional context in comments.

Comments

3

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())

2 Comments

@user3483203 - Thank you.
You can also del list
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.