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

Use of panda .loc function in order to select a specific data within a column

I have a dataset which can be found on this website: http://tennis-data.co.uk/alldata.php. It gathers outcomes of both WTA and ATP tennis games over several years.

I would like to find how many sets did the player “ Federer R.” win during the years 2016 and 2017, and for this I used the .loc function as shown below:

df = df_atp.loc[df_atp["Date"].between("01/01/2016", "31/12/2017"), ['Winner', 'Wsets']]
print(df)

When I print df, here is the result: (part of it since the whole result was very long)
result

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

I think I’m on the right path, but I want to specifically have only Federer on my results, but like the image shows it I have every other players. I have tried to add ["Federer R."] at the end of the .loc function but it only gives me an error.

What could I add to the .loc function in order to have only Federer in the results?

Thank you in advance! 😀

>Solution :

df = df_atp.loc[df_atp["Date"].between("01/01/2016", "31/12/2017"), ['Winner', 'Wsets']]
df = df[df['Winner'] == 'Federer R.']
print(df)

is the most readable way to do it. You could also do

df = df_atp.loc[df_atp["Date"].between("01/01/2016", "31/12/2017") & (df['Winner'] == 'Federer R.'), ['Winner', 'Wsets']]

to do it in one line, but I’d favor the first approach for legibility.

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