简体   繁体   English

熊猫根据相同的ID填充空值

[英]Pandas fill in empty values based on the same ID

I have 2 Dataframe.我有 2 个数据框。

My first data But the second row, column B is missing a value.我的第一个数据 但是第二行 B 列缺少一个值。

在此处输入图片说明

My second data has this value in column B of the second row我的第二个数据在第二行的 B 列中有这个值

在此处输入图片说明

I want the second data to fill the null value of the first data.我希望第二个数据填充第一个数据的空值。

I tried the following code:我尝试了以下代码:

import pandas as pd

test1 ='test1.xlsx'
test2 ='test2.xlsx'

df1 = pd.excel(test1)
df2 = pd.excel(test2)

df3 = pd.merage(df1, df2, on='clolumns', how='left')

df3.to_excel('df3.xlsx')

The result will be columns2_X and columns2_Y , I want to merge into one column.结果将是 columns2_X 和 columns2_Y ,我想合并为一列。

You can try combine_first function你可以试试combine_first函数

>>> df1 = pd.DataFrame({ 'A': [123, 1234, 12345], 'B' : ['str1', None, 'str3']})
>>> df2 = pd.DataFrame({ 'A': [123, 1234, 12345], 'B' : [None, 'str2', None]})
>>> result = df1.combine_first(df2)
>>> result
       A     B
0    123  str1
1   1234  str2
2  12345  str3

One option is use np.where一种选择是使用np.where

df2['columns2_x'] = np.where(df2['columns2_x'] == '', df2['columns2_y'], df2['columns2_x'])

another option is to use update df1 with filetered df2另一种选择是将update df1 与过滤的 df2 一起使用

df1.update(df2[df2['columns1'].isin(df1[df1['columns2'] == '']['columns1'])])

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM