Home > front end >  Python Empty series when filtering data
Python Empty series when filtering data

Time:05-10

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:

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

What am I doing wrong?

CodePudding user response:

== 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
  • Related