Python Forum
Overwhelmed with error message using pandas drop()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Overwhelmed with error message using pandas drop()
#1
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'
buran write Feb-18-2021, 07:21 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
instead of line 8, try:

data = data[data['state'] == 'Tx']
or
data = data[data.state == 'Tx']
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error message about iid from RandomizedSearchCV Visiting 2 934 Aug-17-2023, 07:53 PM
Last Post: Visiting
  pyarrow error when importing pandas sravva 1 886 Jun-06-2023, 05:09 PM
Last Post: snippsat
  Another Error message. the_jl_zone 2 943 Mar-06-2023, 10:23 PM
Last Post: the_jl_zone
  Mysql error message: Lost connection to MySQL server during query tomtom 6 15,685 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  understanding error message krlosbatist 1 1,856 Oct-24-2021, 08:34 PM
Last Post: Gribouillis
  pandas.errors.ParserError: Error tokenizing data. C error: Expected 9 fields in line Anldra12 9 15,090 Jun-15-2021, 08:16 AM
Last Post: Anldra12
  Error message pybits 1 36,066 May-29-2021, 10:26 AM
Last Post: snippsat
  f-string error message not understood Skaperen 4 3,268 Mar-16-2021, 07:59 PM
Last Post: Skaperen
  Winning/Losing Message Error in Text based Game kdr87 2 2,927 Dec-14-2020, 12:25 AM
Last Post: bowlofred
  Don't understand error message Milfredo 2 1,995 Aug-24-2020, 05:00 PM
Last Post: Milfredo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020