简体   繁体   中英

Pandas fill in empty values based on the same ID

I have 2 Dataframe.

My first data But the second row, column B is missing a value.

在此处输入图片说明

My second data has this value in column B of the second row

在此处输入图片说明

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.

You can try combine_first function

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

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

another option is to use update df1 with filetered df2

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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