user14005574
user14005574

Reputation: 1

How to delete rows in a CSV based on string value in a column in Python?

I have a large CSV file that I am trying to delete many rows from.

The column I am making this decision on has many different crops, but I am only interested in a few of the crops and their related data in other columns.

The code I have tried is the following:

import pandas as pd
df = pd.read_csv('C2014.csv')
cropnames = ['Cotton', 'Rice', 'Corn']
for crop in cropnames:
  df = df[df.Nomcultivo !] crop]

The strings in cropnames are the string values in the rows I want in the column "Nomcultivo." I hope that makes sense. Any help would be appreciated!

Upvotes: 0

Views: 660

Answers (2)

Hussain
Hussain

Reputation: 308

You can try this approach: I assume Nomcultivo is the col. which contains cropnames

cropnames = ['Cotton', 'Rice', 'Corn']
df[df.Nomcultivo.isin(cropnames )]

Upvotes: 0

The AG
The AG

Reputation: 690

If I understood you correctly, In your column 'Nomcultivo' you want only cropnames = ['Cotton', 'Rice', 'Corn'], then why not just filter it out?:

import pandas as pd
df = pd.read_csv('C2014.csv')
new = df.Nomcultivo.isin(cropnames)
df[new]

Upvotes: 1

Related Questions