【问题标题】:Compare multiple dataframes' columns names with original one's in Pandas将多个数据框的列名与 Pandas 中的原始列名进行比较
【发布时间】:2021-03-20 13:54:24
【问题描述】:

假设我有一个数据框df,标题为a, b, c, d

我想将其他 dfs (df1, df2, df3, ...) 列名称与它进行比较。我需要dfs 的所有列名应该与df 完全相同(请注意,列名的不同顺序不应视为不同的列名)。

例如:

原始数据框:

df = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
                   columns=['a', 'b', 'c'])

col = ['a', 'b', 'c']

dfs:

df1 = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
                   columns=['a', 'c', 'b'])

返回identical columns name

df2 = pd.DataFrame(np.array([[1, 2, 3, 10], [4, 5, 6, 11], [7, 8, 9, 12]]),
                   columns=['a', 'c', 'e', 'b'])

返回extra columns in dataframe

df3 = pd.DataFrame(np.array([[1, 2], [4, 5], [7, 8]]),
                   columns=['a', 'c'])

返回missing columns in dataframe

df4 = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
                   columns=['a', '*c', 'b'])

返回errors in dataframe's column names

df5 = pd.DataFrame(np.array([[1, 2, 3, 9], [4, 5, 6, 9], [7, 8, 9, 10]]),
                   columns=['a', 'b', 'b', 'c'])

返回extra columns in dataframe

如果太复杂,各种错误返回columns names are incorrect也是可以的。

我怎么能在 Pandas 中做到这一点?谢谢。

【问题讨论】:

  • 请提供具有预期输出的示例数据框
  • 已更新,请查收。

标签: python-3.x pandas list numpy dataframe


【解决方案1】:

我认为在这里设置是不错的选择,因为顺序并不重要:

def compare(df, df1):
    orig = set(df.columns)
    
    c = set(df1.columns)

    #testing if length of set is same like length of columns names
    if len(c) != len(df1.columns):
        return ('extra columns in dataframe')
    #if same sets
    elif (c == orig):
        return ('identical columns name')
    #compared subsets
    elif c.issubset(orig):
        return ('missing columns in dataframe')
    #compared subsets
    elif orig.issubset(c):
        return ('extra columns in dataframe')
    else:
        return ('columns names are incorrect')

print(compare(df, df1))                    
print(compare(df, df2))    
print(compare(df, df3))    
print(compare(df, df4))    
print(compare(df, df5))    

identical columns name
extra columns in dataframe
missing columns in dataframe
columns names are incorrect
extra columns in dataframe

对于返回值:

def compare(df, df1):
    orig = set(df.columns)
    
    c = set(df1.columns)

    #testing if length of set is same like length of columns names
    if len(c) != len(df1.columns):
        col = df1.columns.tolist()
        a = set([str(x) for x in col if col.count(x) > 1])
        return f'duplicated columns: {", ".join(a)}'
    #if same sets
    elif (c == orig):
        return ('identical columns name')
    #compared subsets
    elif c.issubset(orig):
        a = (str(x) for x in orig - c)
        return f'missing columns: {", ".join(a)}'
    #compared subsets
    elif orig.issubset(c):
        a = (str(x) for x in c - orig)
        return f'extra columns: {", ".join(a)}'
    else:
        a = (str(x) for x in c - orig)
        return f'incorrect: {", ".join(a)}'

print(compare(df, df1))                    
print(compare(df, df2))    
print(compare(df, df3))    
print(compare(df, df4))    
print(compare(df, df5)) 

identical columns name
extra columns: e
missing columns: b
incorrect: *c
duplicated columns: b   

【讨论】:

  • @ahbon - 好的,添加了第二个条件,现在使用您的示例数据框进行测试。
  • @ahbon - 是的,因为现在我使用了你的示例数据。
  • 对不起,还有一个问题,检查时是否可以返回缺失、额外和错误的列名?
  • @ahbon - 没那么简单,需要一些时间。
  • 可能返回duplicated columns b
【解决方案2】:

我写了一个普通的python函数,它使用pandas函数来获取列并比较它们,请看看这是否有帮助:

def check_errors(original_df, df1):
    original_columns = original_df.columns
    columns1 = df1.columns
    if len(original_columns) > len(columns1):
       print("Columns missing!!")

    elif len(original_columns) < len(columns1):
       print("Extra Columns")

    else:
        for i in columns1:
           if i not in original_columns:
              print("Column names are incorrect")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-07
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 2018-07-25
    • 1970-01-01
    • 2018-03-14
    相关资源
    最近更新 更多