3

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']

2 Answers 2

3

Use the str string accessor

euro16_df[euro16_df.Team.str[0] == 'G']
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Much appreciated. How is it that it can compare with the original string "Germany" without needing an accessor. I totally missed this while and would love to read more about this.
@Pradyot there are a bunch of methods that can be done pandas does a good job of making the most important ones available without an accessor. Otherwise pandas groups like methods together. There is also a date time/time delta accessor 'dt'. Category accessor 'cat'.
Again. Really appreciate the direction.
1

Also str startswith.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.