Home > Mobile >  trying to drop some columns from dataframe but throwing a name error
trying to drop some columns from dataframe but throwing a name error

Time:04-06

I've read a CSV file into Jupyter Notebook and I'm trying to remove redundant columns I'm not gonna use. I'm using the drop() method but its giving me a NameError! I'm sure the columns exist and I feel Like I'm missing something obvious here but i can't seem to figure it out.

So here is my code so far:

  #Calling Libraries
  import os               # File management
  import pandas as pd     # Data frame manipulation
  import numpy as np      # Data frame operations
  import datetime as dt   # Date operations
  import seaborn as sns   # Data Viz

  flight_df=pd.read_csv(r'C:\Users\pc\Desktop\Work\flights.csv')
  # removing na rows
  flight_df.dropna()
  # dropping redundant columns
  newdf=flight_df.drop([O_COUNTRY,O_LATITUDE,O_LONGITUDE,D_COUNTRY,D_LATITUDE,D_LONGITUDE,SCHEDULED_DEPARTURE,DIVERTED,CANCELLED,CANCELLATION_REASON,TAXI_OUT,TAXI_IN,WHEELS_OFF, WHEELS_ON,SCHEDULED_ARRIVAL],axis=1, inplace = True)

throws this error:

  NameError                                 Traceback (most recent call last)

~\AppData\Local\Temp/ipykernel_15328/4119461383.py in 2 flight_df.dropna() 3 # dropping redundant columns newdf=flight_df.drop([O_COUNTRY,O_LATITUDE,O_LONGITUDE,D_COUNTRY,D_LATITUDE,D_LONGITUDE,SCHEDULED_DEPARTURE,DIVERTED,CANCELLED,CANCELLATION_REASON,TAXI_OUT,TAXI_IN,WHEELS_OFF, WHEELS_ON,SCHEDULED_ARRIVAL],axis=1, inplace = True)

  NameError: name 'O_COUNTRY' is not defined

I have tried to instead define the ones i want to keep but it's giving me the same error

CodePudding user response:

As column names are str in this case, you have to enclose them in str delimiters

newdf=flight_df.drop(['O_COUNTRY','O_LATITUDE','O_LONGITUDE','D_COUNTRY' ...

Warning ! You are using the attribute inplace=True but you try to assing the result to a new variable. This variable will be None. Either write

flight_df.drop(['O_COUNTRY', ...],axis=1,inplace=True)

or

newdf=flight_df.drop(['O_COUNTRY', ...],axis=1)

Same things with your dropna. It won't be stored as you wrote it.

CodePudding user response:

I think you just want to put quotes around the column names. The way you are doing it now Python expects there to be an object named O_COUNTRY.

  • Related