简体   繁体   English

python / pandas - 查找两个数据框之间的公共列,并创建另一个具有相同列的数据框以显示它们的差异

[英]python / pandas - Find common columns between two dataframes, and create another one with same columns showing their difference

My version of pandas is:我的 pandas 版本是:

pd.__version__
'0.25.3'

I have two dataframes , below is a sample, with the majority of the columns being the same across the two dataframes .我有两个dataframes ,下面是一个示例,两个数据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.我试图找到公共columns ,并创建一个新的dataframe ,其中包含显示它们值差异的所有公共columns

A sample from c_r dataframe:来自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

A sample from x dataframe:来自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: (to have the same column names, and show the difference, ie:) new_df:具有相同的列名,并显示差异,即:)

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

I have tried - please see where the error is in the code:我试过了 -请查看代码中的错误位置

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

but for some reason I get back only 1 row of values.但出于某种原因,我只得到 1 行值。

Any ideas of why my code does not work?关于为什么我的代码不起作用的任何想法? How can I achieve it the resulting dataframe, including the initial column of Comp_name ?我怎样才能得到结果 dataframe,包括Comp_name的初始列?

Thanks all.谢谢大家。

Have you tried using intersection/ symmetric_difference(for difference) ie您是否尝试过使用 intersection/ symmetric_difference(for difference) 即

a = dataframe2.columns.intersection(dataframe1.columns)
print(a)
I think I understood the problem now, I have a small code as below.    
   import pandas as pd
    
    d = {'col1': [-0.02  ,  -0.02  ,-0.02  ,-0.02  ], 'col2': [-0.39,   -0.39,  -0.39,  -0.39],'col3': [-0.01,-0.02,-0.01,-0.02]}
    d1 = {'col1': [-0.02  ,  -0.02  ,-0.02  ,-0.02  ], 'col2': [-0.39,   -0.39,  -0.39,  -0.39],'col3': [0.05,0.03,0.06,0.04]}
    
    df = pd.DataFrame(data=d)
    df2 = pd.DataFrame(data=d1)
    
    
    
    df = df.apply(pd.to_numeric, errors='coerce')
    df2 = df2.apply(pd.to_numeric, errors='coerce')
    
    print(df)
    print(df2)
    
    col1  = df.col1 - df2.col1
    col2  = df.col2 - df2.col2
    col3  = df.col3 - df2.col3
    
    dfnew = pd.concat([col1, col2,col3], axis=1)
    
    
    print(type(col1))
    print(dfnew)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM