Pandas DataFrame merge 数据合并

环境信息

  • ModelArts
    • Notebook - Multi-Engine 2.0 (python3)
      • JupyterLab - Notebook - Conda-python3
        • pandas 0.22.0

Pandas DataFrame merge 数据合并

import pandas as pd

# 情景描述
# df1是一个时序序列的数据集
# pd.date_range(start='2021-3-1',periods=7) 生成时间序列
df1 = pd.DataFrame({"SN":["A","B","C","A","A","B","D"],"value_1":[1,2,3,11,111,22,4],"time":pd.date_range(start='2021-3-1',periods=7)})

# df2是一个 计算后得出的结果
# SN 列中都是唯一的,每个SN都有唯一对应的值
df2 = pd.DataFrame({"SN":["A","B","C","E"],"max":[1000,2000,3000,4000]})
df1

df2

# 相同的列名SN进行连接,how='inner'
pd.merge(df1,df2)

# 右连接 df2是完整的
pd.merge(df1,df2,how="right")

# 左连接 df1是完整的
pd.merge(df1,df2,how="left")

# 取并集
pd.merge(df1,df2,how="outer")

help

help(pd.merge)

Help on function merge in module pandas.core.reshape.merge:

merge(left, right, how='inner', on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=False, suffixes=('_x', '_y'), copy=True, indicator=False, validate=None)
    Merge DataFrame objects by performing a database-style join operation by
    columns or indexes.
    
    If joining columns on columns, the DataFrame indexes *will be
    ignored*. Otherwise if joining indexes on indexes or indexes on a column or
    columns, the index will be passed on.
    
    Parameters
    ----------
    left : DataFrame
    right : DataFrame
    how : {'left', 'right', 'outer', 'inner'}, default 'inner'
        * left: use only keys from left frame, similar to a SQL left outer join;
          preserve key order
        * right: use only keys from right frame, similar to a SQL right outer join;
          preserve key order
        * outer: use union of keys from both frames, similar to a SQL full outer
          join; sort keys lexicographically
        * inner: use intersection of keys from both frames, similar to a SQL inner
          join; preserve the order of the left keys

......

备注

  1. 欢迎各位同学一起来交流学习心得^_^
  2. 在线课程、沙箱实验、认证、论坛和直播,其中包含了许多优质的内容,推荐了解与学习。
(完)