zoukankan      html  css  js  c++  java
  • 合并pd.merge

    import pandas as pd
    
    df1 = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],
                         'A': ['A0', 'A1', 'A2', 'A3'],
                         'B': ['B0', 'B1', 'B2', 'B3']})
    df2 = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],
                          'C': ['C0', 'C1', 'C2', 'C3'],
                          'D': ['D0', 'D1', 'D2', 'D3']})
    df3 = pd.DataFrame({'key1': ['K0', 'K0', 'K1', 'K2'],
                        'key2': ['K0', 'K1', 'K0', 'K1'],
                        'A': ['A0', 'A1', 'A2', 'A3'],
                        'B': ['B0', 'B1', 'B2', 'B3']})
    df4 = pd.DataFrame({'key1': ['K0', 'K1', 'K1', 'K2'],
                        'key2': ['K0', 'K0', 'K0', 'K0'],
                        'C': ['C0', 'C1', 'C2', 'C3'],
                        'D': ['D0', 'D1', 'D2', 'D3']})
    print('-'*10,'df1','-'*10)
    print(df1)
    print('-'*10,'df2','-'*10)
    print(df2)
    print('-'*10,'df3','-'*10)
    print(df3)
    print('-'*10,'df4','-'*10)
    print(df4)
    
    print("-----pd.merge(df1, df2, on='key')-----")
    print(pd.merge(df1, df2, on='key'))
    print("-----pd.merge(df3, df4, on=['key1','key2'])-----")
    print(pd.merge(df3, df4, on=['key1','key2']))

     

    # 参数 left_on, right_on, left_index, right_index → 当键不为一个列时,可以单独设置左键与右键
    
    df1 = pd.DataFrame({'lkey':list('bbacaab'),
                       'data1':range(7)})
    df2 = pd.DataFrame({'rkey':list('abd'),
                       'date2':range(3)})
    print(pd.merge(df1, df2, left_on='lkey', right_on='rkey'))
    print('------')
    # df1以‘lkey’为键,df2以‘rkey’为键
    
    df1 = pd.DataFrame({'key':list('abcdfeg'),
                       'data1':range(7)})
    df2 = pd.DataFrame({'date2':range(100,105)},
                      index = list('abcde'))
    print(pd.merge(df1, df2, left_on='key', right_index=True))
    # df1以‘key’为键,df2以index为键
    # left_index:为True时,第一个df以index为键,默认False
    # right_index:为True时,第二个df以index为键,默认False
    
    # 所以left_on, right_on, left_index, right_index可以相互组合:
    # left_on + right_on, left_on + right_index, left_index + right_on, left_index + right_index

  • 相关阅读:
    成都58同城快速租房的爬虫,nodeJS爬虫
    `qs.parse` 的简单实现
    使用windbg定位内存问题【入门级】
    C#正则实现匹配一块代码段
    Zeebe服务学习3-Raft算法与集群部署
    Zeebe服务学习2-状态机
    Zeebe服务学习1-简单部署与实现demo
    C#后端接收前端的各种类型数据
    大话设计模式--单例模式具体使用
    大话设计模式--DI(依赖注入)
  • 原文地址:https://www.cnblogs.com/yunshangyue71/p/13584360.html
Copyright © 2011-2022 走看看