【问题标题】:Compare columns between two different excel files (python, pandas)比较两个不同 excel 文件(python、pandas)之间的列
【发布时间】:2020-08-05 02:48:02
【问题描述】:

我想比较两个不同 Excel 文件的列和行。我想将file1中“Category 2”的值与file2中的“Group”列表进行比较。如果 file2 的值在 file1 中不存在,则从 file2 中删除该行。我一直在环顾四周并尝试了一些不同的事情,但找不到任何特定于我正在尝试做的事情。如有必要,我可以从 file1 中删除诸如“所有列表”和“列表 X”之类的标题,但在比较方面,我真的不知道如何前进。我将不胜感激任何帮助或指示,或者知道这是否可能。谢谢

df1 = pd.read_excel('file1', 'Table 3')
df2 = pd.read_excel('file2.xlsx')

# difference = df1[df1!=df2]
# print(df1.equals(df2))

print(df1['Category 2']).where(df1['Courses']) == df2['Group']

文件1

All Lists
Category 1  Category 2  Category 3  Category 4  Category 5  Category 6                   
List 1                  
            element1        x   
            element2        x   
            element3        x   
            element4        x   
            element5        x   
List 2                  
            card1           x   
            card2           x   
            card3           x   
            card4           x   
            card5           x   
List 3                  
            box1            x   
            box2            x   
            box3            x   
            box4            x   
            box5            x

文件2

Group   Manager     quarter1    quarter2    quarter3    quarter4                total
element2    A           $          $           $           $                      $
notElement  B           $          $           $           $                      $
card3       C           $          $           $           $                      $
box4        D           $          $           $           $                      $
element3    E           $          $           $           $                      $
box1        F           $          $           $           $                      $
notElement  B           $          $           $           $                      $
notElement  C           $          $           $           $                      $             
card7       D           $          $           $           $                      $
element4    E           $          $           $           $                      $

想要的输出:

Group   Manager     quarter1    quarter2    quarter3    quarter4                total
element2    A           $          $           $           $                      $
card3       C           $          $           $           $                      $
box4        D           $          $           $           $                      $
element3    E           $          $           $           $                      $
box1        F           $          $           $           $                      $
card7       D           $          $           $           $                      $
element4    E           $          $           $           $                      $

【问题讨论】:

    标签: python excel pandas dataframe


    【解决方案1】:

    您可以使用Series.isin 来检查df2["Group"] 中的元素是否包含在df1["Category_2"] 中,这将返回一个True/False 系列,当与index 一起应用时可以用来删除不需要的行(df2.drop(df2[notCateg].index)

    df1 = pd.read_excel('file1', 'Table 3')
    df2 = pd.read_excel('file2.xlsx')
    
    notCateg = ~df2["Group"].isin(df1["Category_2"])
    df2 = df2.drop(df2[notCateg].index)
    df2.reset_index(drop=True, inplace=True)
    print(df2)
    
          Group Manager quarter1 quarter2 quarter3 quarter4 total
    0  element2       A        $        $        $        $     $
    1     card3       C        $        $        $        $     $
    2      box4       D        $        $        $        $     $
    3  element3       E        $        $        $        $     $
    4      box1       F        $        $        $        $     $
    5  element4       E        $        $        $        $     $
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-17
      • 2023-04-10
      • 2020-04-24
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      • 2015-10-22
      相关资源
      最近更新 更多