Home > Mobile >  Dropping rows from a CSV file in python
Dropping rows from a CSV file in python

Time:11-16

I trying to drop two rows from a CSV file and after exploring other similar questions I have not been able to figure it out still. I have tried using pandas and it does not seem to be working and I tried a few functions that people on here said worked for them but I could not get to work.

Here is my code I've ben trying. My output is unchanged with and without trying to drop the two rows. The rows I'm trying to drop are the United States and Iowa State rows.

import csv
import pandas as pd

iowa_csv = r'C:\Users\Downloads\Iowa 2010 Census Data Population Income.csv'
with open(iowa_csv, encoding='utf-8-sig') as csvfile:
    reader = csv.DictReader(csvfile)
    county_col = {'County': []}
    for record in reader:
        county_col['County'].append(record['County'])
    print(county_col)


df = pd.read_csv(r'C:\Users\Downloads\Iowa 2010 Census Data Population Income.csv')
df.drop([9, 20])
print(df)

CodePudding user response:

df.drop does not modify the DataFrame inplace, unless you ask it to. It returns a new DataFrame. So, either

df = df.drop([9,20])

or

df.drop([9,20],inplace=True)

CodePudding user response:

df.drop(labels=[9, 21], axis=0, inplace=True)

  • Related