• 合并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

  • 相关阅读:
    TFS 2013”无法移除仍为团队管理员身份的标识”
    如何在TFS的过程模板中添加报表
    集成TFS Build生成与SonarQube获取代码分析结果
    TFS 2015 Update 2功能探索
    使用Azure Automation(自动化)定时关闭和启动虚拟机
    TFS 与活动目录AD(Active Directory)的同步机制
    Eclipse 常用快捷键
    JMeter网站并发性测试
    docker安装配置GitLab
    javaWeb项目在用maven启动时必须要用到的坐标
  • 原文地址:https://www.cnblogs.com/yunshangyue71/p/13584360.html
Copyright © 2020-2023  润新知