简体   繁体   English

用来自另一个数据帧特定列的值替换来自数据帧特定列的 Nan 值

[英]Replacing Nan value from specific column of a dataframe with value from specific column of another dataframe

I have a problem replacing the nan values in one column of the dataframe with column values of other dataframe.我在用其他数据帧的列值替换数据帧的一列中的 nan 值时遇到问题。 Here's test example:这是测试示例:

    Name  Age Name2
0    tom   10   tom
1   nick   15  nick
2   juli   14  juli
3    NaN   12  anne
4  error   17   neo

I want to replace Nan values from column Name (not other columns if there is some Nan values in them) with specific value from other dataframe, for instance Name2 value from this dataframe:我想用来自其他数据帧的特定值替换列 Name 中的 Nan 值(不是其他列,如果其中有一些 Nan 值),例如来自这个数据帧的 Name2 值:

    Name  Age Name2
4  error   17   neo

What I want to get is this:我想得到的是这样的:

    Name  Age Name2
0    tom   10   tom
1   nick   15  nick
2   juli   14  juli
3    neo   12  anne
4  error   17   neo

This is test code for this example:这是此示例的测试代码:

# initialize list of lists 
data = [['tom', 10, 'tom'], ['nick', 15, 'nick'], ['juli', 14, 'juli'], [np.nan, 12, 'anne'], ['error', 17, 'neo']] 

# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Name', 'Age', 'Name2']) 

df1=df[df.Age==np.max(df.Age)]

Then I tried those three methods, but none works - my dataframe always stays with Nan value.然后我尝试了这三种方法,但都不起作用 - 我的数据框始终保持 Nan 值。

df.Name.fillna(df1.Name2, inplace=True)

df.where(df.Name.isnull(), df1.Name2, axis=0)

df[df.Name.isnull()].Name=df1.Name2

Can you tell me where I'm making mistake?你能告诉我我哪里出错了吗?

Here is necessary convert one element Series to scalar:这里有必要将一个元素Series转换为标量:

df.Name.fillna(df1.Name2.iat[0], inplace=True)
#assign output to new DataFrame, test for not missing values
df = df.where(df.Name.notna(), df1.Name2.iat[0])
#use DataFrame.loc for avoid SettingWithCopyWarning
df.loc[df.Name.isnull(), 'Name']=df1.Name2.iat[0]

print (df)
    Name  Age Name2
0    tom   10   tom
1   nick   15  nick
2   juli   14  juli
3    neo   12  anne
4  error   17   neo

Detail :详情

print (df1.Name2)
4    neo
Name: Name2, dtype: object

print (df1.Name2.iat[0])
neo

暂无
暂无

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

相关问题 从另一个数据帧中的列值替换pandas数据帧中的列中的值 - Replacing value in a column in pandas dataframe from a column value in another dataframe Python:在匹配不同列中的值后,用来自另一个数据帧的值替换特定列中的 NaN - Python: Replacing NaN in a specific column by values from another dataframe after matching values in a different column 从另一列列表中的特定值填充一个数据框列 - Fill one Dataframe Column from specific value in list of another column 用从数据框中另一列的最大值计算的值替换字符串 - Replacing string with value calculated from the max of another column in a dataframe 如何根据同一 dataframe 中另一列的值替换 Dataframe 中列中的 NaN 值 - How to replace NaN value in column in Dataframe based on values from another column in same dataframe 在特定列中找到通用值时,用另一个数据框中的值替换一个数据框中的值 - Replace values in a dataframe with values from another dataframe when common value is found in specific column 从另一个数据框的列中为数据框的每一行查找一个特定值 - Finding a specific value for each row in a Dataframe from another Dataframe's column 根据数据框中另一列的条件应用特定函数替换列的值 - Applying a specific function to replace value of column based on criteria from another column in dataframe 根据 dataframe 列中的特定值替换所有值 - Replacing all values based on specific value in column dataframe 用必须在另一列的特定行上计算的公式填充值为0或NaN的Pandas数据框行 - Fill Pandas dataframe rows, whose value is a 0 or NaN, with a formula that have to be calculated on specific rows of another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM