KeyError Place not found in axis

0 votes

Hi Guys,

I have one DataFrame in pandas as given below.

import pandas as pd
df = pd.read_csv('my.csv')
Place State
0 Kolkata WestBengal
1 Delhi Delhi
2 Bangalore Karnataka
3 Kolkata WestBengal
4 Delhi Delhi

I am trying to drop the Place column from this DataFrame using drop() function. But it is showing me the below error.

df.drop('Place')
KeyError                                  Traceback (most recent call last)
<ipython-input-16-5010dfe7568c> in <module>
----> 1 df.drop('Place',axis=0)
~\anaconda3\envs\myenv\lib\site-packages\pandas\core\frame.py in drop(self, labels, axis, index, columns, level, inplace, errors)
   3995             level=level,
   3996             inplace=inplace,
-> 3997             errors=errors,
   3998         )
   3999
~\anaconda3\envs\myenv\lib\site-packages\pandas\core\generic.py in drop(self, labels, axis, index, columns, level, inplace, errors)
   3934         for axis, labels in axes.items():
   3935             if labels is not None:
-> 3936                 obj = obj._drop_axis(labels, axis, level=level, errors=errors)
   3937
   3938         if inplace:
~\anaconda3\envs\myenv\lib\site-packages\pandas\core\generic.py in _drop_axis(self, labels, axis, level, errors)
   3968                 new_axis = axis.drop(labels, level=level, errors=errors)
   3969             else:
-> 3970                 new_axis = axis.drop(labels, errors=errors)
   3971             result = self.reindex(**{axis_name: new_axis})
   3972
~\anaconda3\envs\myenv\lib\site-packages\pandas\core\indexes\base.py in drop(self, labels, errors)
   5015         if mask.any():
   5016             if errors != "ignore":
-> 5017                 raise KeyError(f"{labels[mask]} not found in axis")
   5018             indexer = indexer[~mask]
   5019         return self.delete(indexer)
KeyError: "['Place'] not found in axis"

How can I solve this error?

Jun 24, 2020 in Python by akhtar
• 38,260 points
40,762 views

1 answer to this question.

0 votes

Hi@akhtar,

You need to provide the axis parameter in your drop function. By default, it will take axis=0, which means a row-wise operation. So you have to set axis=1 inside drop function to do a column-wise operation.

df.drop('Place',axis=1,inplace=True)
State
0 WestBengal
1 Delhi
2 Karnataka
3 WestBengal
4 Delhi

I hope this will help you.

answered Jun 24, 2020 by MD
• 95,460 points
I have the same problem. I set the axis=1, it doesn't help. Still KeyError ... not found in axis.
Hi@Nikolay,

Can you share your data frame schema and the command you have used to drop?

Related Questions In Python

0 votes
1 answer

Section postgresql not found in the database.ini file

Python doesn't know what $FILEDIR is. Try an absolute path ...READ MORE

answered Oct 3, 2018 in Python by Priyaj
• 58,020 points
3,686 views
–1 vote
1 answer

nmap.nmap.PortScannerError: 'nmap program was not found in path

python-nmap module used in python happens to use nmap binary ...READ MORE

answered Jan 30, 2019 in Python by Omkar
• 69,220 points
16,022 views
0 votes
1 answer

Installing package not found in conda

You can install pip in your conda env and then run pip ...READ MORE

answered Jul 31, 2019 in Python by SDeb
• 13,300 points
3,772 views
0 votes
1 answer

Error: "Module not found" while importing tensorflow in pycharm.

Try the following: Go to Files -> Settings ...READ MORE

answered Jun 23, 2020 in Python by Sirajul
• 59,230 points
2,478 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,441 views
0 votes
1 answer
0 votes
1 answer

How to delete column from pandas DataFrame?

Hi@akhtar, You can use the del command in ...READ MORE

answered Jun 24, 2020 in Python by MD
• 95,460 points
1,147 views
+1 vote
1 answer

ImportError: DLL load failed: The specified module could not be found.

Hi@akhtar, You may get this error if you ...READ MORE

answered Jun 22, 2020 in Python by MD
• 95,460 points
9,729 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP