简体   繁体   English

比较 python 中的两个 dataframe 列

[英]Compare two dataframe columns in python

I Want to comparing two dataframe columns and thier values.我想比较两个 dataframe 列和它们的值。 If it matches then it Shall be 1 and if not then 0.如果匹配则为 1,如果不匹配则为 0。

How can i do this.?我怎样才能做到这一点。? The dataframe has all the same column name. dataframe 具有所有相同的列名。 I need to check whether the values are matching or otherwise.我需要检查这些值是否匹配。

I'm assuming you're using pandas.我假设您使用的是 pandas。 In your particular case you can do this:在您的特定情况下,您可以这样做:

#Setup
df1 = pd.DataFrame({'col' : [5,4,2,6,1,7]})
df2 = pd.DataFrame({'col' : [3,4,0,6,1,5]})

#Relevant code
(df1["col"] == df2["col"]).astype("int8")

Output Output

0    0
1    1
2    0
3    1
4    1
5    0
Name: col, dtype: int8

Let's consider df1 and df2 as the target dataframes & col as the column of interest.让我们将df1df2视为目标数据框,将col视为感兴趣的列。

import pandas as pd 

def match_columns(df1, df2, col):
    match_list = [] 
    data1_list = df1[col].tolist()
    data2_list = df2[col].tolist() 

    for i in range(len(data1_list)):
        if str(data1_list[i]) ==  str(data2_list[i]):
            match_list.append(0) # matching values marked as '0'
        else:
            match_list.append(1) # non-matching values marked as '1'
    return match_list


if __name__ == "__main__":

    df1 = pd.read_csv('/path/to/csv1', header=0) # row 0 => column headers
    df2 = pd.read_csv('/path/to/csv2', header=0) # row 0 => column headers

    match_list = match_columns(df1,df2, 'TV1')   # match_list for 'TV1'
    print(match_list)

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

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