Khaine775
Khaine775

Reputation: 2765

Why does my DataFrame.loc return the wrong row?

So I have a CSV file looking something like this:

          IncidntNum    Category  
0         150060275     NON-CRIMINAL   
1         150098210          ROBBERY   
2         150098210          ASSAULT 
3         150098213          ASSAULT

I want to get all rows containing a specific Category, e.g. 'ASSAULT', so I've tried to do something like this:

with open("file.csv", 'r') as f:
    data = pd.read_csv(f)
    crimes = data.loc[['Category'] == 'ASSAULT']

But it only gives me one row containing 'NON-CRIMINAL', which is obviously not right. What am I doing wrong when trying to fetch the data?

EDIT: I figured out the problem myself, and turns out I made a typo.

Instead, the line should say:

crimes = data.loc[data['Category'] == 'ASSAULT']

Not I get both rows containing 'ASSAULT'

Upvotes: 1

Views: 818

Answers (2)

Piyush S. Wanare
Piyush S. Wanare

Reputation: 4933

Try this:-

df = pandas.read_csv('csvfile.txt', index_col=False, header=0);
print df[df['Category'] == 'ASSAULT')] #To filter on the basis of single value.
print df[df['A'].isin(['ASSAULT','ROBBERY'])] ##To filter on the basis of multiple values.

Upvotes: 1

Khaine775
Khaine775

Reputation: 2765

I figured out the problem myself, and turns out I made a typo.

Instead, the line should say:

crimes = data.loc[data['Category'] == 'ASSAULT']

Not I get both rows containing 'ASSAULT'.

Upvotes: 1

Related Questions