python/pandas-查找两个数据框之间的公共列,并创建另一个具有相同列的列,以显示它们之间的差异 [英] python / pandas - Find common columns between two dataframes, and create another one with same columns showing their difference

查看:223
本文介绍了python/pandas-查找两个数据框之间的公共列,并创建另一个具有相同列的列,以显示它们之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的熊猫版本是:

pd.__version__
'0.25.3'

我有两个 dataframes ,下面是一个示例,其中大多数列在两个 dataframes 中相同.我试图找到常见的,并创建一个新的 dataframe ,其中包含所有常见的 columns ,以显示其值的差异.

I have two dataframes, below is a sample, with the majority of the columns being the same across the two dataframes. I am trying to find the common columns, and create a new dataframe with all the common columns that shows their difference in values.

来自 c_r 数据帧的示例:

Comp_name        EOL - CL Per $      Access - CL Per $      Total Impact - CL Per $
Nike             -0.02               -0.39                    -0.01
Nike             -0.02               -0.39                    -0.02
Adidas           -0.02               -0.39                    -0.01
Adidas           -0.02               -0.39                    -0.02

来自 x 数据帧的示例:

Comp_name        EOL - CL Per $      Access - CL Per $      Total Impact - CL Per $
Nike             -0.02               -0.39                    0.05
Nike             -0.02               -0.39                    0.03
Adidas           -0.02               -0.39                    0.08
Adidas           -0.02               -0.39                    0.08

new_df:(具有相同的列名,并显示差异,即:)

new_df: (to have the same column names, and show the difference, i.e:)

EOL - CL Per $ - Diff      Access - CL Per $ - Diff      Total Impact - CL Per $ - Diff
-0.00                      -0.00                         -0.06
-0.00                      -0.00                         -0.05
-0.00                      -0.00                         -0.09
-0.00                      -0.00                         -0.10

我已经尝试-请查看代码中的错误:

new_df = pd.DataFrame()

for i in c_r:
    for j in x:
        if c_r[i].dtype != object and x[j].dtype != object:
            if i == j:
               ## THE ISSUE IS IN THE LINE BELOW ##
                new_df[i+'-Diff'] = (c_r[i]) - (x[j])
        
        else:
            pass

但是由于某种原因,我只能返回1行值.

but for some reason I get back only 1 row of values.

关于我的代码为何不起作用的任何想法?我如何实现它的结果数据框,包括 Comp_name 的初始列?

Any ideas of why my code does not work? How can I achieve it the resulting dataframe, including the initial column of Comp_name?

谢谢.

推荐答案

您是否尝试过使用交集/symmetric_difference(用于差异),即

Have you tried using intersection/ symmetric_difference(for difference) i.e.

a = dataframe2.columns.intersection(dataframe1.columns)
print(a)

这篇关于python/pandas-查找两个数据框之间的公共列,并创建另一个具有相同列的列,以显示它们之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆