pandas.DataFrame.combine_first 出现重复行的问题

在某次使用pd.Dataframe.combine_first()时,出现了重复行,经排查原因为第2个df存在重复行,记录一下。

DataFrame.combine_first(other)

  1. Update null elements with value in the same location in other.
  2. Combine two DataFrame objects by filling null values in one DataFrame with non-null values from other DataFrame. The row and column indexes of the resulting DataFrame will be the union of the two.
  3. Parameters
    other : DataFrame
    Provided DataFrame to use to fill null values.
  4. Returns
    DataFrame

combine_first()方法用于将两个DF组合为一个。结果是两个DF的并集,在调用者DF为Null的情况下,将采用传递的DF中的值。如果两个空值在同一索引处,则在该索引处返回空值

df1=pd.DataFrame(np.arange(1,10).reshape(3,3),columns=list('abc'),index=['1#','2#','3#'])
df2=pd.DataFrame(np.arange(10,22).reshape(4,3),columns=list('bcd'),index=['2#','3#','3#','4$'])
print(df1)
print(df2)
print(df1.combine_first(df2,))

由于df2有重复行,所以输出为:

    a  b  c
1#  1  2  3
2#  4  5  6
3#  7  8  9
     b   c   d
2#  10  11  12
3#  13  14  15
3#  16  17  18
4$  19  20  21
      a     b     c     d
1#  1.0   2.0   3.0   NaN
2#  4.0   5.0   6.0  12.0
3#  7.0   8.0   9.0  15.0
3#  7.0   8.0   9.0  18.0
4$  NaN  19.0  20.0  21.0

参考

  1. https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.combine_first.html?highlight=combine_first#pandas.DataFrame.combine_first
  2. https://blog.csdn.net/starter_____/article/details/79198430
  3. https://vimsky.com/examples/usage/python-pandas-series-combine_first.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值