在熊猫中进行数据框串联后获取值不匹配的行位置

时间:2018-11-06 17:38:53

标签: python-3.x pandas dataframe data-science

我有两个数据框,我想对它们进行详细的比较。这意味着我想将df_one中的每一列(及其所有行)的单元格值与df_two进行匹配。目前,我将它们串联到第三个数据帧中,并希望获得每列不匹配的行位置。例如:

df_one看起来像这样-

enter image description here

df_two看起来像这样-

enter image description here

现在我要像这样将它们串联-

df_one.reset_index(drop=True, inplace=True)
df_two.reset_index(drop=True, inplace=True)
df_concat = pd.concat([df_one,df_two],axis=0).fillna('---')

连接后的输出-

enter image description here

这就是我要从这一切中得到的-

A)我想在两个数据帧之间的每一列之间进行逐行比较(连接后),并获取它们不同的行位置。

SAMPLE OUTPUT -- (When comparing column Month)

Row positions with mismatch - 0,1,2,3,4,5,6 

SAMPLE OUTPUT -- (When comparing column Year)

No mismatch found between columns Year in both datasets

SAMPLE OUTPUT -- (When comparing column Day)

No mismatch found between columns Day in both datasets

SAMPLE OUTPUT -- (When comparing column Country)

Row positions with mismatch - 0,1,2,3,4,5,6

B)另外,是否有一种方法可以添加标签或其他方式以串联形式将数据帧彼此区分开?

注意:执行此操作的另一种方法(不使用concat)将是检查df_two中是否存在df_one中的Day列,然后遍历所有行-然后对df_1中的所有列进行此操作。但是我认为,如果df_one中有200列,而df_two中有250列,那将是非常低效的。

谢谢!

0 个答案:

没有答案