pandas - 将均值应用于分组数据框中的特定行

时间:2018-02-03 13:24:35

标签: python pandas

我一直在摸不着头脑。

我有一个如下所示的数据框:

将pandas导入为pd     导入numpy为np

d={'gameweek': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 2, 9: 2, 10: 2, 11: 2, 12: 2, 13: 2, 14: 2, 15: 2, 16: 3, 17: 3, 18: 3, 19: 3, 20: 3, 21: 3, 22: 3, 23: 3}, 'match_id': {0: 0, 1: 0, 2: 1, 3: 1, 4: 2, 5: 2, 6: 3, 7: 3, 8: 4, 9: 4, 10: 5, 11: 5, 12: 6, 13: 6, 14: 7, 15: 7, 16: 8, 17: 8, 18: 9, 19: 9, 20: 10, 21: 10, 22: 11, 23: 11}, 'points': {0: 48.0, 1: np.nan, 2: 41.0, 3: 40.0, 4: 55.0, 5: 50.0, 6: 38.0, 7: 45.0, 8: 40.0, 9: 37.0, 10: 29.0, 11: np.nan, 12: 43.0, 13: 15.0, 14: 46.0, 15: 43.0, 16: 24.0, 17: 45.0, 18: 40.0, 19: 20.0, 20: 45.0, 21: np.nan, 22: 49.0, 23: 35.0}, 'name': {0: 'team1', 1: 'Average', 2: 'team2', 3: 'team3', 4: 'team4', 5: 'team5', 6: 'team6', 7: 'team7', 8: 'team2', 9: 'team7', 10: 'team5', 11: 'Average', 12: 'team1', 13: 'team3', 14: 'team6', 15: 'team4', 16: 'team5', 17: 'team1', 18: 'team7', 19: 'team3', 20: 'team6', 21: 'Average', 22: 'team2', 23: 'team4'}}
df = pd.DataFrame.from_dict(data=d)

df.head()

    gameweek    match_id    name    points
0   1   0   team1   48.0
1   1   0   Average     NaN
2   1   1   team2   41.0
3   1   1   team3   40.0
4   1   2   team4   55.0

我想做的是更换所有的NaNs'与分组游戏周列的平均值。

我可以弄清楚如何使用groupby函数来获得平均值,例如

df['points'].groupby(by=df['gameweek']).mean()

gameweek
1    45.285714
2    36.142857
3    36.857143
Name: points, dtype: float64

但我努力将其应用于每个游戏周的正确领域。柱。我已经看过使用' fillna'但无法确定如何使用特定值定位特定行。

任何帮助将不胜感激。非常感谢。

0 个答案:

没有答案