Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 熊猫-替换NaN值_Python_Pandas - Fatal编程技术网

Python 熊猫-替换NaN值

Python 熊猫-替换NaN值,python,pandas,Python,Pandas,在以下df_目标中,我看到所有nan都可以被其上方或下方的单元格值替换,从而消除“团队”的重复行值: For Team Goals Home Goals Away Color 0 Arsenal NaN 1.17 #EF0107 1 Arsenal 1.70 NaN #EF0107 2

在以下
df_目标
中,我看到所有
nan
都可以被其上方或下方的单元格值替换,从而消除“团队”的重复行值:

                    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

我该怎么做?

首先检查
groupby
+

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

如果要删除所有NaN行:

df\u goals=df\u goals.dropna(axis=0)

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

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()