8

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.

3
  • Can you contextualise some more? By adding come code, for example. Commented Feb 8, 2017 at 14:52
  • 1
    Was added , thanks Commented Feb 8, 2017 at 15:17
  • My problem was that I used pd.read_csv(..., header=None) and forgot about it. I know it's not the case here but if somebody has similar problem just check if you're using header=None ;-). Commented Nov 19, 2018 at 22:14

2 Answers 2

13

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
Sign up to request clarification or add additional context in comments.

3 Comments

But the code line is: df.drop(['a'], 1, inplace=True) . Can you note the code for dropping the first column please
I think you are doing something wrong with your sample file. Confirm that the files is correctly formatted and that the file you think you are using is is your working directory. If that checks out, output your DataFrame to confirm it is the way you expect.
Thanks Rob . It seems that the problem was encoding . I saved the file as(utf-8) while it should have been saved as ANSI .
2
bad= pd.read_csv('bad_modified.csv')
A=bad.sample(n=10)
B=bad.drop(A.index,axis=0)

This is an example of dropping a dataframe partly. In case you need it.

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.