ScienceParkStudyGroup / studyGroup

Gather together a group to skill-share, co-work, and create community
https://www.scienceparkstudygroup.info
Other
6 stars 12 forks source link

Use an if statement in the lambda function (function apply / Pandas / Python) #14

Closed StefaniAstrologo closed 6 years ago

StefaniAstrologo commented 6 years ago

Hi studyGroup! I have a big data frame of values that are log 2 transformed. I wanted to transform back just using (2)**x as follow:

df = result[['RM_CD44neg','RM_CD44pos','WM_CD44neg','WM_CD44pos']].apply(lambda x: (2)**x)

BUT! if I have zero in the original data frame this formula will give me a false positive ( (2)x = 1) I thought I could use in alternative the fallowing: df = result[['RM_CD44neg','RM_CD44pos','WM_CD44neg','WM_CD44pos']].apply(lambda x: (((2)x)-1.0) if x == 0 else (2)**x)

Of course does not work!

ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', u'occurred at index RM_CD44neg')

Please help!

pietromarchesi commented 6 years ago

Why do you need to do all columns at once? Just iterate through your columns and fix them one at a time with your lambda. There may be nicer ways but this will work.

import numpy as np
import pandas as pd
result = pd.DataFrame(np.array([[1, 2], [3, 0], [0, 6]]), columns=['a', 'b'])

df = result.copy()
for column in ['a', 'b']:
    df[column] = result[column].apply(lambda x: (((2)**x)-1.0) if x == 0 else (2)**x)
StefaniAstrologo commented 6 years ago

Thanks

I think my df is some how wrong. At least now I know the function is correct!