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

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

问题描述

我的熊猫版本是:

pd.__version__
'0.25.3'

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

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 数据帧的示例:

A sample from c_r dataframe:

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 数据帧的示例:

A sample from x dataframe:

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?

谢谢大家.

推荐答案

你有没有试过使用intersection/symmetric_difference(for difference) i.e.

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

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

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

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