Pradyot
Pradyot

Reputation: 3059

Conditional Select on a dataframe column

Hi I am wondering how conditional select on a pandas column works. In the Below code

In [162]: euro16
Out[162]: {'Goals': [16, 8], 'Team': ['Germany', 'England']}

In [163]: euro16_df = pd.DataFrame(euro16)

In [164]: euro16_df[euro16_df.Team == 'Germany']
Out[164]:
   Goals     Team
0     16  Germany

However when you try a conditional on the team that involved string access ie: Say all teams starting with 'G'. I get a KeyError.I would greatly appreciate any information on what might be happening here.

euro16_df[euro16_df.Team[0] == 'G']

Upvotes: 3

Views: 372

Answers (2)

piRSquared
piRSquared

Reputation: 294218

Use the str string accessor

euro16_df[euro16_df.Team.str[0] == 'G']

Upvotes: 3

Shenglin Chen
Shenglin Chen

Reputation: 4554

Also str startswith.

euro16_df[euro16_df.Team.str.startswith('G')]

Upvotes: 1

Related Questions