Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use combine and combine_first functions in pandas

Shulou Source: shulou.com Published: 2022-06-01 03:00:59 10月04日 Update

This article will explain in detail how to use combine and combine_first functions in pandas. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Combine

In the process of data merging, we may need to calculate the values of the corresponding positions. Pandas provides combine and combine_first function methods for cooperative operations in this respect.

Df.combine (other: 'DataFrame', func, fill_value=None, overwrite:' bool' = True,)-> 'DataFrame'

For example, when merging data, take the smallest value of the cell.

In [79]: df1 = pd.DataFrame ({'Aids: [0,0],' bundles: [4,4]}) In [80]: df2 = pd.DataFrame ({'Aids: [1,1],' bones: [3,3]}) In [81]: df1Out [81]: a B0041 10 4In [82]: df2Out [82]: a B01 31 1 3In [83]: take_smaller = lambda S1, S2: S1 if s1.sum ()

< s2.sum() else s2In [84]: df1.combine(df2, take_smaller)Out[84]: A B0 0 31 0 3# 也可以调用numpy的函数In [85]: import numpy as npIn [86]: df1.combine(df2, np.minimum)Out[86]: A B0 0 31 0 3 fill_value填充缺失值 In [87]: df1 = pd.DataFrame({'A': [0, 0], 'B': [None, 4]})In [87]: df2 = pd.DataFrame({'A': [1, 1], 'B': [3, 3]})In [88]: df1Out[88]: A B0 0 NaN1 0 4.0In [89]: df2Out[89]: A B0 1 31 1 3In [90]: df1.combine(df2, take_smaller, fill_value=-88)Out[90]: A B0 0 -88.01 0 4.0 overwrite=False保留 In [91]: df1 = pd.DataFrame({'A': [0, 0], 'B': [4, 4]})In [92]: df2 = pd.DataFrame({'B': [3, 3], 'C': [-10, 1], }, index=[1, 2])In [93]: df1Out[93]: A B0 0 41 0 4In [94]: df2Out[94]: B C1 3 -102 3 1In [95]: df1.combine(df2, take_smaller)Out[95]: A B C0 NaN NaN NaN1 NaN 3.0 -10.02 NaN 3.0 1.0# 保留A列原有的值In [96]: df1.combine(df2, take_smaller, overwrite=False)Out[96]: A B C0 0.0 NaN NaN1 0.0 3.0 -10.02 NaN 3.0 1.0 另外一个combine_first df.combine_first(other: 'DataFrame') ->

'DataFrame'

When the element in df is empty, it is replaced by the one in other, and the result is union merging.

In [97]: df1 = pd.DataFrame ({'Aids: [None, 0],' Barrier: [None, 4]}) In [98]: df2 = pd.DataFrame ({'Aids: [1,1],' baked: [3] 3]}) In [99]: df1Out [99]: a B0 NaN NaN1 0.0 4.0In [100]: df2Out [100]: a B0 1 31 11 3In [101]: df1.combine_first (df2) Out [101]: a B0 1.03.01 0.0 4.0In [102]: df1 = pd.DataFrame ({'Aids: [None, 0],' Bread: [4] None]}) In [103]: df2 = pd.DataFrame ({'Barrier: [3,3],' index=: [1,1]}, index= [1] In: df1Out: a B0 NaN 4.01 0.0 NaNIn: df2Out: BC1 1312 3 1In: df1.combine_first (df2) Out: B B C0 NaN 4.0 NaN1 0.0 3.0 1.02 NaN 3.0 this is the end of the article on "how to use combine and combine_first functions in pandas" Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Tags: Function article data more good practical minimum location element content article method time knowledge result missing process faceted reference Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Apple OPPO Reno Linux MariaDB