I Want to comparing two dataframe columns and thier values. If it matches then it Shall be 1 and if not then 0.
How can i do this.? The dataframe has all the same column name. I need to check whether the values are matching or otherwise.
I'm assuming you're using 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
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.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.