sumathi
sumathi

Reputation: 55

How to iterate dataframe row and compare the values in if condition

Here is my code, where I am trying to compare the values of two dataframes column wise and increment by 1. The code gives the error below error. Kind request to help me here.

df_select = pd.read_csv(path,low_memory=False)
df_datahub = pd.read_csv(path1,low_memory=False)
df_select_i = df_select.set_index('medical_event_vod__c')
df_datahub_i = df_datahub.set_index('intact_id')

i, j, k, l, m = 0, 0, 0, 0, 0
for index, row in df_select_i.iterrows():
    m += 1
    if index in df_datahub_i.index:
        i += 1
        df_temp_select = df_select_i.loc[index]
        df_temp_datahub = df_datahub_i.loc[index]

        if  df_temp_select['createddate'] == df_temp_datahub['src_sys_cr8_ts'] :
            k+=1
        else:
            j+=1
    else:
        l += 1
print(m)
print(i)
print(j)
print(k)
print(l)

This is the error:

if  df_temp_select['createddate'] == df_temp_datahub['src_sys_cr8_ts'] :
raise ValueError(
    ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or 
    a.all().

Upvotes: 0

Views: 124

Answers (2)

Anthony
Anthony

Reputation: 135

When you compare two Series (such as your columns) with ==, it compares them element-wise and returns a Series. As the error suggests, you can use .all() on the returned Series to check if all the comparisons are True.

Example:

In [1]: import pandas as pd

In [2]: x = pd.Series([1,2,3])

In [3]: y = pd.Series([1,3,2])

In [4]: x == y
Out[4]: 
0     True
1    False
2    False
dtype: bool

In [5]: (x == y).all()
Out[5]: False

Upvotes: 0

Rahman Fahd
Rahman Fahd

Reputation: 33

The problem here is that you are comparing a pd.Series with a value, so you'll have multiple True and multiple False values, as in the case above. This of course is ambiguous, since the condition is neither True or False.

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Upvotes: 1

Related Questions