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:
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.
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
