0

I have a dataframe as below. I am trying to check if there is a nan in the Liq_Factor, if yes, put 1 otherwise divide use/TW. Result in column Testing.

+---+------------+------------+--------+--------+--------+
| 1 |            | Liq_Factor | Zscire | Use    | Tw     |
| 2 | 01/10/2020 | 36.5       | 44     | 43.875 | 11.625 |
| 3 | 02/10/2020 | Nan        | 43.625 | 13.625 | 33.25  |
| 4 | 03/10/2020 | 6.125      | 47.875 | 22.5   | 4.625  |
| 5 | 04/10/2020 | Nan        | 34.25  | 37.125 | 36     |
| 6 | 05/10/2020 | 43.875     | 17.375 | 5.5    | 36.25  |
| 7 | 06/10/2020 | 40         | 14.125 | 21.125 | 14.875 |
| 8 | 07/10/2020 | 42.25      | 44.75  | 21.25  | 31.75  |
+---+------------+------------+--------+--------+--------+

I was wondering if i can use .apply in the sense of

DF1['Testing']=(DF1['Liq_Factor'].apply(lambda x: x=1 if pd.isna(DF1['Zscore']) else DF1['Use']/DF1['Tw'])

Can you please help?

Thanks, H

2 Answers 2

2

You can use apply or another alternative is where function from numpy:

df['Liq_Factor'] = np.where(df['Liq_Factor'] == np.Nan, 1, df['Use']/df['TW'])

Following your comments below you can do:

# create another column with the calculation
df['calc'] = (1/3)* df['ATV']/df['TW']*100000000
# create two rules (you can use one rule and then the opposite)
mask_0 = (df['calc'] < 1)
mask_1 = (df['calc'] > 1)
# change result value by condition
df.loc[mask_0, 'Liq Factor'] = df['calc']
df.loc[mask_1, 'Liq Factor'] = 1
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you so much! It works but i have two questions please. 1. Is there a way to do it with pandas? 2. If yes, What if i have more than one condition? Can this be done in pandas?
For your first question posted the answer using pandas, for second question you can create a function with your input columns and calculation logic with many conditions, call that function using apply.
When I added the "min" I get the below error ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). My line code is as below Final['Liq Factor'] = np.where(Final['ATV'] == np.nan, 1, min((1/3)*(Final['ATV']/Final['TW']*1000000000),1)) Any help please?
You can not do it like this, because df['ATV'] for example, is not a single number but a whole column. Let me write a solution for you. A few minutes please
@Hassanov use min(a,b) to get the minimum, its working fine for me- I have updated it in my answer as well.
|
1

Use below code-

df['Testing']=df.apply(lambda x: 1 if x['Liq_Factor']=='Nan'  else x['Use']/x['Tw'], axis=1)

Based on changes in comment section

df['Testing']=df.apply(lambda x: 1 if x['Liq_Factor']=='Nan'  else min(x['Use']/x['Tw'],1), axis=1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.