【问题标题】:Comparing the content a List of pandas DataFrames比较熊猫数据框列表的内容
【发布时间】:2016-05-04 19:18:27
【问题描述】:

我有一个包含三个熊猫数据框的列表。所有 DataFrame 都具有确切的列名并具有相同的长度。我想比较每个 DataFrame 中特定列的所有条目。假设列表有:

List=[df1,df2,df3].

每个dataFrame都有以下结构。 df1 有结构

column1    column2   column3
  4          3          4
  4          5          7
  7          6          6
  8          6          4

df2 有结构

column1    column2   column3
  4          3          4
  7          5          7
  7          6          5
  8          6          4

df3 有结构

column1    column2   column3
  4          3          5
  4          1          7
  7          6          6
  8          6          4

我想将 df1 column1 和 column2(每行)的内容与包含 df2(column1 和 column2)和 df3(column1 和 column2)进行比较

我写了一些类似这样的想法:

for i in range(len(List)):# iterate through the list
    for j in range(len(List[0].index.values)):# iterate through the the whole dataFrame
    #I would like to so something like: if df1[column1][row1]=df2[column1][row1] then do ....
    # now i dont know how to iterate through all the dataFrames simulatanously to compare the content of of column 1 and column 2(for each row k) of df1 with the content of column 1 and column 2 of df2 and column 1 and column 2 of df3.

我被困在那里

【问题讨论】:

    标签: python python-2.7 pandas dataframe


    【解决方案1】:

    首先,使用提供的数据创建数据框

    import pandas as pd
    
    df1 = pd.DataFrame({
        'column1': [4,4,7,8],
        'column2': [3,5,6,6],
        'column3': [4,7,6,4]
    })
    print(df1)
    #    column1  column2  column3
    # 0        4        3        4
    # 1        4        5        7
    # 2        7        6        6
    # 3        8        6        4
    
    df2 = df1.copy()
    df2['column1'][1] = 7
    df2['column3'][2] = 5
    print(df2)
    #    column1  column2  column3
    # 0        4        3        4
    # 1        7        5        7
    # 2        7        6        5
    # 3        8        6        4
    
    df3 = df1.copy()
    df3['column2'][1] = 1
    df3['column3'][0] = 5
    print(df3)
    #    column1  column2  column3
    # 0        4        3        5
    # 1        4        1        7
    # 2        7        6        6
    # 3        8        6        4
    

    然后,获取相同形状的数据框,并带有一个布尔值指示哪个 两个数据帧中的条目相等

    print(df1.eq(df2))
    #    column1  column2  column3
    # 0     True     True     True
    # 1    False     True     True
    # 2     True     True    False
    # 3     True     True     True
    

    获取一系列布尔值,指示哪些列的所有 两个数据帧中的对应行相等

    print(df1.eq(df2).all())
    # column1    False
    # column2     True
    # column3    False
    # dtype: bool
    

    获取一系列布尔值,指示所有对应的行 两个数据框中的列相等

    print(df1.eq(df2).all(axis='columns'))
    # 0     True
    # 1    False
    # 2    False
    # 3     True
    # dtype: bool
    

    获取单个布尔值,指示所有对应条目是否相等 在两个数据框中

    print(df1.equals(df2))
    # False
    

    如果你需要组合每一对数据帧并进行比较,你可以使用

    from itertools import combinations
    List = [df1, df2, df3]
    for a, b in combinations(enumerate(List, 1), 2):
        print(f'df{a[0]}.equals(df{b[0]}): ', a[1].equals(b[1]))
    # df1.equals(df2):  False
    # df1.equals(df3):  False
    # df2.equals(df3):  False
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-28
      • 1970-01-01
      • 2020-06-08
      • 2019-08-19
      • 2021-10-08
      • 1970-01-01
      • 1970-01-01
      • 2015-08-11
      相关资源
      最近更新 更多