熊猫-替换NaN值

时间:2020-07-11 02:18:13

标签: python pandas

在下面的df_goals中,我看到所有NaNs都可以由其上方或下方的单元格值替换,可以消除重复的行值'For Team':

                    For Team  Goals Home  Goals Away    Color
0                    Arsenal         NaN        1.17  #EF0107
1                    Arsenal        1.70         NaN  #EF0107
2                Aston Villa         NaN        1.10  #770038
3                Aston Villa        1.45         NaN  #770038
4                Bournemouth         NaN        0.77  #D3151B
5                Bournemouth        1.17         NaN  #D3151B
6   Brighton and Hove Albion         NaN        1.00  #005DAA
7   Brighton and Hove Albion        1.45         NaN  #005DAA
8                    Burnley         NaN        1.25  #630F33
9                    Burnley        1.33         NaN  #630F33
10                   Chelsea         NaN        1.82  #034694
11                   Chelsea        1.11         NaN  #034694
12            Crystal Palace         NaN        0.89  #C4122E
13            Crystal Palace        0.79         NaN  #C4122E
14                   Everton         NaN        1.30  #274488
15                   Everton        1.40         NaN  #274488
16            Leicester City         NaN        2.25  #0053A0
17            Leicester City        2.00         NaN  #0053A0
18                 Liverpool         NaN        2.00  #CE1317
19                 Liverpool        2.62         NaN  #CE1317
20           Manchester City         NaN        2.25  #97C1E7
21           Manchester City        2.73         NaN  #97C1E7
22         Manchester United         NaN        0.92  #E80909
23         Manchester United        1.82         NaN  #E80909
24          Newcastle United         NaN        0.67  #231F20
25          Newcastle United        1.10         NaN  #231F20
26              Norwich City         NaN        0.56  #00A14E
27              Norwich City        1.36         NaN  #00A14E
28          Sheffield United         NaN        0.88  #E52126
29          Sheffield United        0.83         NaN  #E52126
30               Southampton         NaN        1.42  #ED1A3B
31               Southampton        1.15         NaN  #ED1A3B
32         Tottenham Hotspur         NaN        1.20  #132257
33         Tottenham Hotspur        1.90         NaN  #132257
34                   Watford         NaN        0.83  #FBEE23
35                   Watford        0.90         NaN  #FBEE23
36           West Ham United         NaN        1.09  #7C2C3B
37           West Ham United        1.83         NaN  #7C2C3B
38   Wolverhampton Wanderers         NaN        1.40  #FDB913
39   Wolverhampton Wanderers        1.33         NaN  #FDB913

我该怎么做?

2 个答案:

答案 0 :(得分:1)

groupby + first进行确认

df=df.groupby('For Team').first()

答案 1 :(得分:0)

如果要删除所有NaN行:

no_nan_list = df_goals.index.tolist() nan_list = df_goals.drop(no_nan_list).apply(lambda x: to_numeric(x,errors='coerce')) df_goals = nan_list.fillna(0) df.head()

您可以将所有Nan值设置为0:

Failed to compile.

./styles/checkout.ts:32:46
Syntax error: Unterminated template

  30 |       border-radius: 15px;
  31 |       &:hover {
> 32 |         background: ${lighten(0.1, '#00BFFF')};
     |                                               ^
  33 |     }
  34 |     }
  35 |     


> Build error occurred
Error: > Build failed because of webpack errors
    at build (C:\Users\null\fodase\node_modules\next\dist\build\index.js:13:917)