Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

ErrorCorner

ErrorCorner Logo ErrorCorner Logo

ErrorCorner Navigation

  • Home
  • Contact Us
  • About Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Contact Us
  • About Us
Home/ Questions/Q 611
Next
Answered
Kenil Vasani
Kenil Vasani

Kenil Vasani

  • 646 Questions
  • 567 Answers
  • 77 Best Answers
  • 26 Points
View Profile
  • 1
Kenil Vasani
Asked: December 14, 20202020-12-14T21:07:08+00:00 2020-12-14T21:07:08+00:00In: Python

Pandas: Dataframe.Drop – ValueError: labels [‘id’] not contained in axis

  • 1

Attempting to drop a column from a DataFrame in Pandas. DataFrame created from a text file.

import pandas as pd 
df = pd.read_csv('sample.txt')
df.drop(['a'], 1, inplace=True)

However, this generates the following error:

ValueError: labels ['a'] not contained in axis      

Here is a copy of the sample.txt file :

a,b,c,d,e
1,2,3,4,5
2,3,4,5,6
3,4,5,6,7
4,5,6,7,8

Thanks in advance.

pandaspython
  • 1 1 Answer
  • 10 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    1. Kenil Vasani

      Kenil Vasani

      • 646 Questions
      • 567 Answers
      • 77 Best Answers
      • 26 Points
      View Profile
      Best Answer
      Kenil Vasani
      2020-12-14T21:02:23+00:00Added an answer on December 14, 2020 at 9:02 pm

      So the issue is that your “sample.txt” file doesn’t actually include the data you are trying to remove.

      Your line

      df.drop(['id'], 1, inplace=True) 
      

      is attepmting to take your DataFrame (which includes the data from your sample file), find the column where the value is ‘id’ in the first row (axis 1) and do an inplace replace (modify the existing object rather than create a new object missing that column, this will return None and just modify the existing object.).

      The issue is that your sample data doesn’t include a column with a header equal to ‘id’.

      In your current sample file, you can only to a drop where the value in axis 1 is ‘a’, ‘b’, ‘c’, ‘d’, or ‘e’. Either correct your code to drop one of those values or get a sample files with the correct header.

      The documentation for Pandas isn’t fantastic, but here is a good example of how to do a column drop in Pandas: http://chrisalbon.com/python/pandas_dropping_column_and_rows.html

      ** Below added in response to Answer Comment from @saar

      Here is my example code:
      Sample.txt:

      a,b,c,d,e
      1,2,3,4,5
      2,3,4,5,6
      3,4,5,6,7
      4,5,6,7,8
      

      Sample Code:

      import pandas as pd
      
      df = pd.read_csv('sample.txt')
      print('Current DataFrame:')
      print(df)
      df.drop(['a'], 1, inplace=True)
      print('\nModified DataFrame:')
      print(df)
      

      Output:

      >>python panda_test.py
      Current DataFrame:
         a  b  c  d  e
      0  1  2  3  4  5
      1  2  3  4  5  6
      2  3  4  5  6  7
      3  4  5  6  7  8
      
      Modified DataFrame:
         b  c  d  e
      0  2  3  4  5
      1  3  4  5  6
      2  4  5  6  7
      3  5  6  7  8
      
      • 7
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Sidebar

    Ask A Question
    • Popular
    • Kenil Vasani

      SyntaxError: invalid syntax to repo init in the AOSP code

      • 5 Answers
    • Kenil Vasani

      xlrd.biffh.XLRDError: Excel xlsx file; not supported

      • 3 Answers
    • Kenil Vasani

      Homebrew fails on MacOS Big Sur

      • 3 Answers
    • Kenil Vasani

      runtimeError: package fails to pass a sanity check for numpy ...

      • 3 Answers
    • Kenil Vasani

      Unable to resolve dependency tree error when installing npm packages

      • 2 Answers

    Explore

    • Most Answered
    • Most Visited
    • Most Voted
    • Random

    © 2020-2021 ErrorCorner. All Rights Reserved
    by ErrorCorner.com