Home > Enterprise >  Object type is not callable in is dataframe where I want to remove rows based on a list of variables
Object type is not callable in is dataframe where I want to remove rows based on a list of variables

Time:12-17

Within my data set on US elections I have a bunch of data in my file which I would like to remove at a row level based on the variable in the "party" column. Sample of my data set;

state county candidate party total_votes won
Delaware Kent County Joe Biden DEM 44552 True
Delaware Kent County Donald Trump REP 41009 False
Delaware Kent County Jo Jorgensen LIB 1044 False
Delaware Kent County Howie Hawkins GRN 420 False
Delaware New Castle County Joe Biden DEM 195034 True
Delaware New Castle County Donald Trump REP 88364 False
Delaware New Castle County Jo Jorgensen LIB 2953 False
Delaware New Castle County Howie Hawkins GRN 1282 False
Delaware Sussex County Donald Trump REP 71230 True

For all rows where the party value is not "DEM" or "REP" I want to delete these. Using the following code I extracted all unique party values;

    uniqueParty = df['party'].unique()
    print(uniqueParty)
['DEM' 'REP' 'LIB' 'GRN' 'WRI' 'PSL' 'IND' 'ALI' 'CST' 'ASP' 'OTH' 'UTY'
 'LLC' 'SWP' 'BAR' 'PRO' 'NON' 'PRG' 'UNA' 'BMP' 'GOP' 'BFP' 'APV' 'IAP'
 'LLP' 'SEP']

I then created a list removing DEM and REP;

uniqueParty2 =['LIB', 'GRN', 'WRI', 'PSL', 'IND', 'ALI', 'CST', 'ASP', 'OTH', 'UTY',
 'LLC', 'SWP', 'BAR', 'PRO', 'NON', 'PRG', 'UNA', 'BMP', 'GOP', 'BFP', 'APV', 'IAP',
 'LLP', 'SEP']


print(type(uniqueParty2))

This is a list I then want to pass through the following code to remove the rows as required;

df = pd.DataFrame(list(uniqueParty2()))
df = df[df.column_name.isin(uniqueParty2) == False]

This is failing and the message I get is "TypeError: 'list' object is not callable" I have tried a tuple and a string type in the code but same message - these types are not callable in the code.

CodePudding user response:

Looks like it's because you have parentheses after uniqueParty2 in df = pd.DataFrame(list(uniqueParty2())). uniqueParty2 is a list, and parentheses tells Python to try call it as a function.

CodePudding user response:

I actually approached this from a different angle. I used the drop function for any values that were not the values I required and that way I didn't need to use a list to get the desired result.

df = df.drop(df[(df.party != 'REP') & (df.party != 'DEM')].index)
  • Related