user8699243
user8699243

Reputation:

KeyError: False pandas dataframe

Look at the following code:

import pandas as pd
data = pd.read_csv("list.csv")
class_names = data.classname.unique()
for ic in class_names:
print(data['classname' == ic])

It says "KeywordError: False" at print(data['classname' == ic])

But it prints the output if classname value is given directly as shown below

print(data['classname'] == 'c1')

What could be the problem?

Upvotes: 2

Views: 3281

Answers (2)

Mohit Motwani
Mohit Motwani

Reputation: 4792

If you want to print data related to a particular classname try:

for ic in class_names:
    print(data[data['classname'] == ic]])

It will return the dataframe with the ic classname

data['classname']==ic will only return a boolean series

Upvotes: 1

Rarblack
Rarblack

Reputation: 4664

The location of the square bracket is placed in the wrong place.

print(data['classname'] == ic)

Upvotes: 2

Related Questions