Dominic Naimool
Dominic Naimool

Reputation: 313

How to create conditional columns in Pandas Data Frame in which column values are based on other columns

I am new to python, I am attempting what would be a conditional mutate in R DPLYR. In short I would like to create a new column in the Data-frame called Result where : if df.['test'] is greater than 1 df.['Result'] equals the respective df.['count'] for that row, if it lower than 1 then df.['Result'] is

df.['count'] *df.['test'] 

I have tried df['Result']=df['test'].apply(lambda x: df['count'] if x >=1 else ...) Unfortunately this results in a series, I have also attempted to write small functions which also return series

I would like the final Dataframe to look like this...

no_ Test Count  Result
1   2    1      1
2   3    5      5
3   4    1      1
4   6    2      2
5   0.5  2      1

Upvotes: 1

Views: 431

Answers (3)

cosmic_inquiry
cosmic_inquiry

Reputation: 2684

Here is a way to do this:

import pandas as pd
df = pd.DataFrame(columns = ['Test', 'Count'], 
                  data={'Test':[2, 3, 4, 6, 0.5], 'Count':[1, 5, 1, 2, 2]})

df['Result'] = df['Count']
df.loc[df['Test'] < 1, 'Result'] = df['Test'] * df['Count']

Output:

       Test  Count  Result
0   2.0      1     1.0
1   3.0      5     5.0
2   4.0      1     1.0
3   6.0      2     2.0
4   0.5      2     1.0

Upvotes: 1

Scott Boston
Scott Boston

Reputation: 153510

You can use np.where:

df['Result'] = np.where(df['Test'] > 1, df['Count'], df['Count'] * df['Test'])

Output:

   No_  Test  Count  Result
0    1   2.0      1     1.0
1    2   3.0      5     5.0
2    3   4.0      1     1.0
3    4   6.0      2     2.0
4    5   0.5      2     1.0

Upvotes: 2

Nakor
Nakor

Reputation: 1514

You can work it out with lists comprehensions:

df['Result'] = [ df['count'][i] if df['test'][i]>1 else 
                 df['count'][i] * df['test'][i] 
                 for i in range(df.shape[0]) ]

Upvotes: 1

Related Questions