Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python: how can I access an element in a pandas series contained in a dictionary

I have the following dictionary dict_group containing several pandas series as value whose keys are a string of group name:

{'Group A':    Pos         Team  Pld  W  D  L  GF  GA  GD  Pts
 0    1  Netherlands    3  2  1  0   5   1  +4    7
 1    2      Senegal    3  2  0  1   5   4  +1    6
 2    3      Ecuador    3  1  1  1   4   3  +1    4
 3    4    Qatar (H)    3  0  0  3   1   7  −6    0,
 'Group B':    Pos           Team  Pld  W  D  L  GF  GA  GD  Pts
 0    1        England    3  2  1  0   9   2  +7    7
 1    2  United States    3  1  2  0   2   1  +1    5
 2    3           Iran    3  1  0  2   4   7  −3    3
 3    4          Wales    3  0  1  2   1   6  −5    1,
 'Group C':    Pos          Team  Pld  W  D  L  GF  GA  GD  Pts
 0    1     Argentina    3  2  0  1   5   2  +3    6
 1    2        Poland    3  1  1  1   2   2   0    4
 2    3        Mexico    3  1  1  1   2   3  −1    4
 3    4  Saudi Arabia    3  1  0  2   3   5  −2    3,
 'Group D':    Pos       Team  Pld  W  D  L  GF  GA  GD  Pts
 0    1     France    3  2  0  1   6   3  +3    6
 1    2  Australia    3  2  0  1   3   4  −1    6
 2    3    Tunisia    3  1  1  1   1   1   0    4
 3    4    Denmark    3  0  1  2   1   3  −2    1,

each series contains a ‘Pos’ column and a ‘Team’ column, I am trying to access the Pos corresponding to a selected team, example if I chose Senegal in group A I want to retrieve the value 2, can you please help me figure this out

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If select in dictionary of DataFrames by key get DataFrame, then convert Team to index, so possible use DataFrame.loc:

df = d['Group A']

out = df.set_index('Team').loc['Senegal', 'Pos']

Or use DataFrame.loc with boolean indexing – but then necessary convert one element Series to scalar:

out = df.loc[df['Team'].eq('Senegal'), 'Pos'].iat[0]

out = next(iter(df.loc[df['Team'].eq('Senegal'), 'Pos']), 'no match')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading