user14054515
user14054515

Reputation:

Getting warning when filtering data in a DataFrame

I want to open a file, filter its data, and show it to the user.

Here is the code:

import pandas as pd
df = pd.read_csv(<file path>)
data = df["Unique Number"] == UID # Unique Number is a column and UID is a Variable
print(data)

And I get a warning (maybe an error)

FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  res_values = method(rvalues)

Any help?

Upvotes: 1

Views: 59

Answers (1)

Arty
Arty

Reputation: 16747

The reason of problem is that I think your UID is string. And in csv some or all of uids are numbers (ints). So to compare later strings and ints they both should be of same common type (type of UID which is string). Specify type string for "Unique Number" field on reading.

df = pd.read_csv('test.csv', dtype = {'Unique Number': str})

You may run corrected code here online with test example.

If your csv contains only integer uids then another (better) solution would be to convert UID variable from string to int like UID = int(UID). Then specifying dtype in read_csv() is not needed.

Upvotes: 0

Related Questions