【问题标题】:Using Merge on a column and Index in Pandas在 Pandas 中对列和索引使用合并
【发布时间】:2015-10-10 07:16:25
【问题描述】:

我有两个独立的数据框,它们共享一个项目编号。在type_df 中,项目号是索引。在time_df 中,项目号是一列。我想计算type_df 中具有Project Type2 的行数。我正在尝试使用pandas.merge() 来做到这一点。它在使用两列时效果很好,但不是索引。我不确定如何引用索引以及merge 是否是正确的方法。

import pandas as pd
type_df = pd.DataFrame(data = [['Type 1'], ['Type 2']], 
                       columns=['Project Type'], 
                       index=['Project2', 'Project1'])
time_df = pd.DataFrame(data = [['Project1', 13], ['Project1', 12], 
                               ['Project2', 41]], 
                       columns=['Project', 'Time'])
merged = pd.merge(time_df,type_df, on=[index,'Project'])
print merged[merged['Project Type'] == 'Type 2']['Project Type'].count()

错误:

名称“索引”未定义。

期望的输出:

2

【问题讨论】:

    标签: python python-2.7 pandas merge


    【解决方案1】:

    如果您想在合并中使用索引,您必须指定left_index=Trueright_index=True,然后使用left_onright_on。对你来说,它应该是这样的:

    merged = pd.merge(type_df, time_df, left_index=True, right_on='Project')
    

    【讨论】:

      【解决方案2】:

      您必须在每个数据框中具有相同的列才能合并。

      在这种情况下,只需为 type_df 创建一个“项目”列,然后在其上合并:

      type_df['Project'] = type_df.index.values
      merged = pd.merge(time_df,type_df, on='Project', how='inner')
      merged
      #    Project  Time Project Type
      #0  Project1    13       Type 2
      #1  Project1    12       Type 2
      #2  Project2    41       Type 1
      
      print merged[merged['Project Type'] == 'Type 2']['Project Type'].count()
      2
      

      【讨论】:

        【解决方案3】:

        另一种解决方案是使用DataFrame.join:

        df3 = type_df.join(time_df, on='Project')
        

        对于版本pandas 0.23.0+the on, left_on, and right_on parameters may now refer to either column names or index level names

        left_index = pd.Index(['K0', 'K0', 'K1', 'K2'], name='key1')
        left = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
                            'B': ['B0', 'B1', 'B2', 'B3'],
                             'key2': ['K0', 'K1', 'K0', 'K1']},
                            index=left_index)
                            
        right_index = pd.Index(['K0', 'K1', 'K2', 'K2'], name='key1')
        
        right = pd.DataFrame({'C': ['C0', 'C1', 'C2', 'C3'],
                             'D': ['D0', 'D1', 'D2', 'D3'],
                             'key2': ['K0', 'K0', 'K0', 'K1']},
                              index=right_index)
                  
        print (left)    
               A   B key2
        key1             
        K0    A0  B0   K0
        K0    A1  B1   K1
        K1    A2  B2   K0
        K2    A3  B3   K1
                
        print (right)
               C   D key2
        key1             
        K0    C0  D0   K0
        K1    C1  D1   K0
        K2    C2  D2   K0
        K2    C3  D3   K1
        

        df = left.merge(right, on=['key1', 'key2'])
        print (df)
               A   B key2   C   D
        key1                     
        K0    A0  B0   K0  C0  D0
        K1    A2  B2   K0  C1  D1
        K2    A3  B3   K1  C3  D3
        

        【讨论】:

        • 我可以传递列的数字索引而不是列名吗?我有重复的列名,因此这个失败了。
        • 令人困惑。当前版本的join没有left_on和right_on。
        猜你喜欢
        • 2021-12-07
        • 2017-08-05
        • 2020-11-22
        • 2020-04-24
        • 2019-12-09
        • 2016-11-22
        • 2014-01-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多