Python过滤数据时为空系列
Python Empty series when filtering data
我有以下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'}})
我想使用以下代码过滤数据以创建滚动图:
Y_for = df[(df["team"] == "América") & (df["variable"] == "xG_for")]["value"].reset_index(drop = True)
但是当我 运行 代码时,我得到一个空系列:
Series([], Name: value, dtype: float64)
我做错了什么?
==
需要完全匹配,但您有尾随空格 ('América '
),用 str.strip
:
去除它们
Y_for = df[(df["team"].str.strip() == "América")
& (df["variable"] == "xG_for")]["value"].reset_index(drop = True)
Y_for
或使用str.contains
:
Y_for = df[ df["team"].str.contains("América")
& (df["variable"] == "xG_for")]["value"].reset_index(drop = True)
Y_for
输出:
0 1.53
1 1.40
2 1.58
Name: value, dtype: float64
我有以下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'}})
我想使用以下代码过滤数据以创建滚动图:
Y_for = df[(df["team"] == "América") & (df["variable"] == "xG_for")]["value"].reset_index(drop = True)
但是当我 运行 代码时,我得到一个空系列:
Series([], Name: value, dtype: float64)
我做错了什么?
==
需要完全匹配,但您有尾随空格 ('América '
),用 str.strip
:
Y_for = df[(df["team"].str.strip() == "América")
& (df["variable"] == "xG_for")]["value"].reset_index(drop = True)
Y_for
或使用str.contains
:
Y_for = df[ df["team"].str.contains("América")
& (df["variable"] == "xG_for")]["value"].reset_index(drop = True)
Y_for
输出:
0 1.53
1 1.40
2 1.58
Name: value, dtype: float64