Conditional selection on dataframe column
Hey. I am wondering how the conditional select works on a pandas column. In the code below
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 to fulfill the condition for a command that includes access to strings, that is: Tell all commands starting with "G". I am getting KeyError.I really appreciate any information on what might be going on here.
euro16_df[euro16_df.Team[0] == 'G']
+3
Pradyot
source
to share
2 answers
Use accessory str
euro16_df[euro16_df.Team.str[0] == 'G']
+3
piRSquared
source
to share
Str is also run.
euro16_df[euro16_df.Team.str.startswith('G')]
+1
Shenglin chen
source
to share