如何根据同一数据框中另一列的值替换数据框中列中的 NaN 值

How to replace NaN value in column in Dataframe based on values from another column in same dataframe

下面是我正在使用的数据框。我想使用 'Country' 和 'Sectors'

列中的值替换 'Score' 列中的 NaN 值
   Country Sectors  Score
0      USA    MECH    NaN
1      IND    ELEC   10.0
2      USA    CHEM    NaN
3      RUS     ENT   34.0
4      PAK    MATH   45.0
5       SL     LAN   23.0
6      USA    CHEM   56.0
7      IND    ELEC   32.0
8      USA    CHEM    NaN
9      RUS     ENT   45.0
10     PAK    MATH   45.0

下面是我试过的代码


import pandas as pd
import numpy as np
df = pd.read_csv('../Data/Data.csv')
df['Score'] = df[(df['Country'] == 'USA') & (df['Sectors'] == 'CHEM') & (df['Score'].isnull())]['Score'].fillna(10)
print(df)

```but I am getting below result```

   Country Sectors  Score
0      USA    MECH    NaN
1      IND    ELEC    NaN
2      USA    CHEM   10.0
3      RUS     ENT    NaN
4      PAK    MATH    NaN
5       SL     LAN    NaN
6      USA    CHEM    NaN
7      IND    ELEC    NaN
8      USA    CHEM   10.0
9      RUS     ENT    NaN
10     PAK    MATH    NaN

我只想替换特定于国家 == 'USA' 和部门 == 'CHEM' 的 NaN 值,并保持所有值不变。有人可以帮忙吗?```

您可以使用 np.where:

>>> df = pd.DataFrame({'Country':['USA', 'IND','USA'], 'Sectors':['MECH', 'ELEC','CHEM'], 'Score':[45.0, 30.0, np.NaN]})
>>> df["Score"] = np.where((df["Country"]=='USA') & (df['Sectors'] == 'CHEM'), 10, df["Score"])
>>> df
  Country Sectors  Score
0     USA    MECH   45.0
1     IND    ELEC   30.0
2     USA    CHEM   10.0

如果df["Country"]=='USA'df['Sectors'] == 'CHEM',则df['Score']设置为10,否则,设置df['Score']中的原始值。