我有以下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)
我究竟做錯了什么?
uj5u.com熱心網友回復:
==需要完全匹配,但您有尾隨空格 ( '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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/473875.html
標籤:Python 熊猫 matplotlib
