KeyError: False pandas dataframe

Viewed 2752

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?

2 Answers

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

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

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

Related