Krish1992
Krish1992

Reputation: 81

Applying 0/1 to boolean logic

I have dataset as below, CURRENT YEAR dataset:

Year ROA Borrowings
2020 1.2 23681

Previous YEAR dataset:

Year ROA Borrowings
2019 2.3 24682

So 2 dataset with different year.I dont want to combine dataset.

I am checking for boolean logic as below,

for key6, data6 in bank.items():   
 cy = data6[data6['index']=='2020']
py = data6[data6['index']=='2019']

ROA_FS = cy['ROA'].apply(lambda x:1 if x>0 else 0)
CFO_FS = cy['CashfromOperatingActivity'].apply(lambda x:1 if x>0 else 0)
C_ROA_FS = (cy['ROA']>py['ROA']).apply(lambda x:1 if x==True else 0)

First two lines in for loop works perfectly as its output is integer. but 3rd line I am comparing two different df's, after that I converted to integer and float of the ROA columns as follows,

 (int(cy['ROA'])>int(py['ROA'])).apply(lambda x: 1 if x=='True' else 0)

When I applied it says following error,

'bool' object has no attribute 'apply'

Please note that I am comparing different ROA of different years

Expected output, change_in_ROA = a['ROA']>b['ROA'], if true print 1 else 0. So output should be 0/1.

Thanks

Upvotes: 1

Views: 66

Answers (1)

jezrael
jezrael

Reputation: 862511

If same number of rows between both DataFrames use:

a['new'] = np.where(a['ROA'].astype(int) > b['ROA'].astype(int), 1, 0)

If not same rows is necessary first join DataFrames together, e.g. by column col (depends by data and what need) and then test:

df = a.merge(b, on='col')

df['new'] = np.where(df['ROA'].astype(int) > df['ROA'].astype(int), 1, 0)

Upvotes: 2

Related Questions