Home > Enterprise >  pandas.Dataframe.loc() and pandas.Dataframe.drop() not working
pandas.Dataframe.loc() and pandas.Dataframe.drop() not working

Time:01-25

I am using pandas to create a ranklist. I created a csv file and used pandas to create a Dataframe. When I am slicing the dataframe using iloc method, its working fine, but its showing error with loc method. Similar error is also shown with drop method. When I am dropping the first column "Name", it works fine, but it again doesn't work with the remaining columns.

import pandas
#creating CRL
file1 = pandas.read_csv("csvfile_of_ranklist2.csv")
print(file1.index)
print(file1.columns)
file2 = file1.drop("Name",axis=1)
print(file1)
print(file2)

It gave the following output:

C:\Users\VIVEK PANDYA\Documents\Python programs>E:/python.exe "c:/Users/VIVEK PANDYA/Documents/Python programs/ranklist.py"
RangeIndex(start=0, stop=8, step=1)
Index(['Name', ' Category', ' Maths', ' Physics', ' Chemistry', 'Unnamed: 5'], dtype='object')
              Name  Category   Maths   Physics   Chemistry Unnamed: 5
0     Vivek Pandey        C1      89        78         100        NaN
1    Prateek Singh        C3     100        75          68
2     Yuvraj Singh        C3      88        92          36        NaN
3  Shaiqua Parveen        C2     100        52          32        NaN
4      Sana Afshan        C1      78        85          21        NaN
5    Shabber Islam        C2      89        23          56        NaN
6    Brahma Pandey        C4      78        54          62        NaN
7    Santosh Singh        C1      85        64          92        NaN
   Category   Maths   Physics   Chemistry Unnamed: 5
0        C1      89        78         100        NaN
1        C3     100        75          68
2        C3      88        92          36        NaN
3        C2     100        52          32        NaN
4        C1      78        85          21        NaN
5        C2      89        23          56        NaN
6        C4      78        54          62        NaN
7        C1      85        64          92        NaN

However if I try to drop "Category", it produces error:

import pandas
#creating CRL
file1 = pandas.read_csv("csvfile_of_ranklist2.csv")
print(file1.index)
print(file1.columns)
file2 = file1.drop("Category",axis=1)
print(file1)
print(file2)

C:\Users\VIVEK PANDYA\Documents\Python programs>E:/python.exe "c:/Users/VIVEK PANDYA/Documents/Python programs/ranklist.py"
RangeIndex(start=0, stop=8, step=1)
Index(['Name', ' Category', ' Maths', ' Physics', ' Chemistry', 'Unnamed: 5'], dtype='object')
Traceback (most recent call last):
  File "c:\Users\VIVEK PANDYA\Documents\Python programs\ranklist.py", line 8, in <module>
    file2 = file1.drop("Category",axis=1)
  File "E:\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "E:\lib\site-packages\pandas\core\frame.py", line 4906, in drop
    return super().drop(
  File "E:\lib\site-packages\pandas\core\generic.py", line 4150, in drop
    obj = obj._drop_axis(labels, axis, level=level, errors=errors)
  File "E:\lib\site-packages\pandas\core\generic.py", line 4185, in _drop_axis
    new_axis = axis.drop(labels, errors=errors)
  File "E:\lib\site-packages\pandas\core\indexes\base.py", line 6017, in drop
    raise KeyError(f"{labels[mask]} not found in axis")
KeyError: "['Category'] not found in axis"

Similarly, if i try to drop the indexes or slice indexes or columns using labels, it produces 'index not found in axis' or 'column not found in axis' error. I tried looking for any solution in pandas documentation, but I found no solution. How can this be solved?

CodePudding user response:

Get column names by file1.columns. Copy and use these column names. Your column names probably have whitespaces in the beginning or in the end.

CodePudding user response:

' Category' and most of the other column names have a leading whitespace. It says in the error KeyError: "['Category'] not found in axis", because 'Category' isn't a columnname.

A simple way to tidy up your column names is:

file.columns = [x.strip() for x in file.columns]

Now your Pandas Dataframe has columns without the leading whitespace and using 'Category' does work.

  •  Tags:  
  • Related