0

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!

  • 1
    Are you looking for: https://stackoverflow.com/questions/19960077/how-to-filter-pandas-dataframe-using-in-and-not-in-like-in-sql – ALollz Jul 27 '20 at 20:30

2 Answers2

1

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]
The AG
  • 672
  • 9
  • 18
0

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

cropnames = ['Cotton', 'Rice', 'Corn']
df[df.Nomcultivo.isin(cropnames )]
Hussain
  • 308
  • 2
  • 7