简体   繁体   English

如何根据同一 dataframe 中另一列的值替换 Dataframe 中列中的 NaN 值

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

Below is the Dataframe i'm working.下面是我正在工作的 Dataframe。 I want to replace NaN values in 'Score' columns using values from column 'Country' and 'Sectors'我想使用“国家”和“部门”列中的值替换“分数”列中的 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

Below is the code which I've tried下面是我尝试过的代码


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

I want to replace only NaN values specific to country == 'USA' and Sectors == 'CHEM' and keep all values as it is.我只想替换特定于国家 == 'USA' 和 Sectors == 'CHEM' 的 NaN 值,并保持所有值不变。 Could anyone please help?```有人可以帮忙吗?```

You can use np.where :您可以使用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

If df["Country"]=='USA' and df['Sectors'] == 'CHEM' , the df['Score'] is set to 10 , else, the original value in df['Score'] is set.如果df["Country"]=='USA'df['Sectors'] == 'CHEM' ,则df['Score']设置为10 ,否则df['Score']中的原始值为放。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pandas Dataframe基于键列,将NaN值替换为以前的值 - pandas Dataframe Replace NaN values with with previous value based on a key column 如何根据条件用NaN替换数据框列值? - How to replace a dataframe column values with NaN based on a condition? 如何基于另一列的NaN值设置熊猫数据框中的值? - How set values in pandas dataframe based on NaN values of another column? 将 dataframe 的一列中的 nan 值替换为第二个 dataframe 的另一列值 - Replace nan values in one column of a dataframe with another column value of second dataframe Python pandas 用模式(同一列 -A)相对于 Pandas 数据帧中的另一列替换一列(A)的 NaN 值 - Python pandas replace NaN values of one column(A) by mode (of same column -A) with respect to another column in pandas dataframe 当同一行中的另一列为NaN时,如何从熊猫数据框中选择特定的列值? - How to select a particular column value from a pandas dataframe when another column in the same row is NaN? 如何从数据框列中检查和替换 NaN - How to check and replace NaN from a dataframe column Pandas Dataframe 用 B 列的值替换 A 列的 NaN 值 - Pandas Dataframe replace NaN values of column A with values from column B 根据条件从另一个 dataframe 值替换列的值 - Python - Replace values of a column from another dataframe values based on a condition - Python 根据另一列中的“NaN”值在 Pandas Dataframe 中创建一个新列 - Create a new column in Pandas Dataframe based on the 'NaN' values in another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM