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:
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'] |