简体   繁体   English

比较 2 个不同数据框的列

[英]Compare columns of 2 different Data frames

I have 2 data frames df1 and df2, which have same column names but could have different order,我有 2 个数据框 df1 和 df2,它们具有相同的列名,但顺序可能不同,

I am validating if these 2 dataframes are identical or not.我正在验证这 2 个数据帧是否相同。

I want to compare these dataframes based on the column names.我想根据列名比较这些数据框。

df1:
A   B   C ...
1   1   1
1   2   4
5   3   8

df2:
A   C   B ....  
1   1   1
1   4   2
5   8   3

I want to compare df1.A & df2.A and so on .我想比较 df1.A & df2.A 等等。

would really appreciate if I could get help regarding the same ,如果我能得到同样的帮助,我将不胜感激,

Thank you谢谢

First, sort both dataframe columns lexicographically,首先,按字典顺序对两个数据框列进行排序,

df1 = df1.reindex_axis(sorted(df.columns), axis=1)
df2 = df2.reindex_axis(sorted(df.columns), axis=1)

then compare.然后比较。

df1 == df2

You don't need iteration.你不需要迭代。 (Which means way faster) (这意味着更快)

如果您不关心按顺序排列的列,而只关心查看它们是否都包含在每个数据框中:

set(df1.columns) == set(df2.columns)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何比较python中两个不同数据框的列? - how to compare columns of two different data frames in python? 如何比较两个不同数据帧的列并保持公共值 - How to compare columns of two different data frames and keep the common values 如何比较具有相同列但行数不同的两个数据帧? - How to compare two data frames with same columns but different number of rows? 如何比较两个不同数据框中的两列并计算出现次数 - how to compare two columns in two different data frames and count the occurrences 比较两个不同的熊猫数据框中的两列值 - compare two columns values in two different pandas data frames 并排比较相同列但不同行的两个数据框 - Compare two data frames side by side for same columns but different rows 比较不同数据框中的两个相似列,并为两个数据框取未合并的行 - Compare two similar columns in different data frames and take unmerged rows for both data frame 如何比较两个不同数据框中的列并保留第一个数据框中的值? - How to compare columns from two different Data Frames and keep the values from the first Data Frame? 我如何基于相同的ID比较两个不同数据框中的日期列 - how do i compare date columns in two different data frames based on the same ID 如果找到匹配项,则比较两个不同数据框中的列,将电子邮件从df2复制到df1 - Compare columns in two different data frames if match found copy email from df2 to df1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM