Python Forum
Overwhelmed with error message using pandas drop() - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Overwhelmed with error message using pandas drop() (/thread-32574.html)



Overwhelmed with error message using pandas drop() - EmmaRaponi - Feb-18-2021

Dear Forum,

I just loaded pandas and am overwhelmed by error messages. I using an input file (type CSV) which contains the following columns: report_dt, sex, age_group, race_ethnicity, county and state and has more than 1 million rows. I am seeking to eliminate all rows except for the state of Texas (Tx). I have attempted to use drop() to accomplish this but am not accomplishing the objective, as written.

My code is:
import os
import glob
import pandas as pd

# Change the directory path to your data input directory
os.chdir("D:\Data Files")
data = pd.read_csv('My_Records.csv')
data.drop(labels=['state' == 'Tx'],axis=0,)
print (data.loc[:, ['report_dt', 'sex', 'age_group', 'race_ethnicity', 'county', 'state']])
data.to_csv("D:\MyRecords_subset(Texas).csv",index=False)
The error messages are:
Error:
Warning (from warnings module): File "<string>", line 1 DtypeWarning: Columns (6) have mixed types.Specify dtype option on import or set low_memory=False. Traceback (most recent call last): File "Files_subset(Texas).py", line 16, in <module> data.drop(labels=['res_state' == 'Tx'],axis=0,) File "C:\Users\ERap\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\frame.py", line 4305, in drop return super().drop( File "C:\Users\ERap\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\generic.py", line 4152, in drop obj = obj._drop_axis(labels, axis, level=level, errors=errors) File "C:\Users\ERap\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\generic.py", line 4187, in _drop_axis new_axis = axis.drop(labels, errors=errors) File "C:\Users\ERap\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 5591, in drop raise KeyError(f"{labels[mask]} not found in axis") KeyError: '[False] not found in axis'



RE: Overwhelmed with error message using pandas drop() - buran - Feb-18-2021

instead of line 8, try:

data = data[data['state'] == 'Tx']
or
data = data[data.state == 'Tx']