【问题标题】:Find missing column names between two dataframes查找两个数据框之间缺少的列名
【发布时间】:2022-09-28 17:09:33
【问题描述】:

我想从 df2 中找到 df1 中未显示的列名 将熊猫导入为 pd

data1 = {\'first_column\':  [\'1\', \'2\', \'2\'],
        \'second_column\': [\'1\', \'2\', \'2\'],
       \'third_column\':[\'1\', \'2\', \'2\'],
        \'fourth_column\':[\'1\', \'2\', \'2\']
        }

df1 = pd.DataFrame(data1)

print(df1)


test = df1[\'second_column\'].nunique()


data2 = {\'first_column\':  [\'2\', \'2\'],
        \'second_column\': [\'2\', \'2\'],
       \'third_column\':[\'2\', \'2\'],
        }

df2 = pd.DataFrame(data2)

print(df2)

我试过not_in_columns=df2.columns[~df2.columns.isin(df1.columns)] 但它不起作用

    标签: python pandas dataframe comparison


    【解决方案1】:

    您可以使用:

    df1.columns.difference(df2.columns)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-30
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 2020-12-03
      • 1970-01-01
      相关资源
      最近更新 更多