Python Spark
Python Spark

Reputation: 303

compare two pandas columns of mixed data datatypes

i have a dataframe as follows, column A and Refer has values of types str,float and int. i have to compare and create a new column if both values same then pass or else fail. it is very simple if all values string datatype but before compare any numeric value in column A must be rounded of, if its a decimal and ends with .0 example in row 3 '1.0' must be changed to '1' before compare with Refer column

       A  Refer
0    usa    usa
1      1      1
2  india    usa
3    1.0      1
4    1.1    1.1
5    1.1    1.2
6  0.888  0.898
7  0.888  0.888

and the out put i am expecting is:

       A  Refer verdict
0    usa    usa    pass
1      1      1    pass
2  india    usa    fail
3    1.0      1    pass
4    1.1    1.1    pass
5    1.1    1.2    fail
6  0.888  0.898    fail
7  0.888  0.888    pass

so i want to create a function so that it will check each row and if row value is numeric then it will check type is float/int. if it is float and ends with '.0' then trunk/remove decimal else continue with flat values. if row value is string then its a stright forward and compare.

can anyone please help

Upvotes: 0

Views: 2086

Answers (1)

BENY
BENY

Reputation: 323326

IIUC, there are lot of build in function in pandas

Update to_numeric

df.apply(pd.to_numeric,errors='ignore',axis=1).nunique(1).eq(1).map({True:'Pass',False:'Fail'})
Out[272]: 
0    Pass
1    Pass
2    Fail
3    Pass
4    Pass
5    Fail
6    Fail
7    Pass
dtype: object

After assign it back

df['verdict']=df.apply(pd.to_numeric,errors='ignore',axis=1).nunique(1).eq(1).map({True:'Pass',False:'Fail'})
df
Out[274]: 
       A  Refer verdict
0    usa    usa    Pass
1      1      1    Pass
2  india    usa    Fail
3    1.0      1    Pass
4    1.1    1.1    Pass
5    1.1    1.2    Fail
6  0.888  0.898    Fail
7  0.888  0.888    Pass

Upvotes: 1

Related Questions