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 Empty series when filtering data

I have the following DF:

pd.DataFrame({'Fecha': {0: '2022-05-01',
  1: '2022-04-24',
  2: '2022-04-21',
  3: '2022-04-16',
  4: '2022-04-10'},
 'team': {0: 'América ',
  1: 'Tigres UANL ',
  2: 'América ',
  3: 'Club Tijuana ',
  4: 'América '},
 'opponent': {0: 'Cruz Azul',
  1: 'América',
  2: 'León',
  3: 'América',
  4: 'Juárez'},
 'variable': {0: 'xG_for', 1: 'xG_for', 2: 'xG_for', 3: 'xG_for', 4: 'xG_for'},
 'value': {0: 1.53, 1: 0.47, 2: 1.4, 3: 0.65, 4: 1.58},
 'venue': {0: 'H', 1: 'H', 2: 'H', 3: 'H', 4: 'H'}})

I want to filter the data to create a rolling plot with the following code:

Y_for = df[(df["team"] == "América") & (df["variable"] == "xG_for")]["value"].reset_index(drop = True)

But when I run the code I get an empty series:

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

Series([], Name: value, dtype: float64)

What am I doing wrong?

>Solution :

== requires an exact match but you have trailing spaces ('América '), strip them with str.strip:

Y_for = df[(df["team"].str.strip() == "América")
         & (df["variable"] == "xG_for")]["value"].reset_index(drop = True)
Y_for

or use str.contains:

Y_for = df[ df["team"].str.contains("América")
         & (df["variable"] == "xG_for")]["value"].reset_index(drop = True)
Y_for

output:

0    1.53
1    1.40
2    1.58
Name: value, dtype: float64
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