Home > database >  Create series based on two pandas dataframe bool columns
Create series based on two pandas dataframe bool columns

Time:01-11

How do I create a series based on two pandas dataframe bool columns?

    round_up  round_down is_round_up is_round_down High     Low
0   0.75       0.7       False       True          0.70532  0.69818
1   0.75       0.7       False       True          0.70196  0.67268
2   0.75       0.7       False       True          0.71243  0.69938
3   0.75       0.7       False       True          0.70226  0.69884
4   0.75       0.7       False       True          0.70292  0.69952
5   0.75       0.7       True        True          0.75100  0.69000

Desired output is a series with round_up if is_round_up or round_down if is_round_down or if both True select is_round_up.

0 0.70
1 0.70
2 0.70
3 0.70
4 0.70
5 0.75

Test data

df = pd.DataFrame({'round_up': {0: 0.75,
  1: 0.75,
  2: 0.75,
  3: 0.75,
  4: 0.75,
  5: 0.75},
 'round_down': {0: 0.70,
  1: 0.70,
  2: 0.70,
  3: 0.70,
  4: 0.70,
  5: 0.70},
 'is_round_up': {0: False, 1: False, 2: False, 3: False, 4: False, 5:True},
 'is_round_down': {0: True, 1: True, 2: True, 3: True, 4: True, 5:True},
 'High': {0: 0.70532, 1: 0.70196, 2: 0.71243, 3: 0.70226, 4: 0.70292, 5:0.751},
 'Low': {0: 0.69818, 1: 0.67268, 2: 0.69938, 3: 0.69884, 4: 0.69952, 5:0.69}})

Edit: To clarify if both columns are False I would expect to see Nan for that row.

CodePudding user response:

Select the columns in order of choice (up is preferred to down in case of both True), mask the False values, bfill and get the first column.

Advantage: the solution is scalable to any number of columns.

(df[['round_up', 'round_down']]
 .where(df[['is_round_up', 'is_round_down']].values)
 .bfill(axis=1)
 .iloc[:,0]
 .rename('result')
)

output:

0    0.70
1    0.70
2    0.70
3    0.70
4    0.70
5    0.75
Name: result, dtype: float64

CodePudding user response:

Use np.select ([condition], [choices])

condition=[df['is_round_up'],df['is_round_down'],df['is_round_up']|df['is_round_down']]
choice =[df['round_up'],df['round_down'],df['round_up']]
df['new']=np.select(condition, choice)



  round_up  round_down  is_round_up  is_round_down     High      Low   new
0      0.75         0.7        False           True  0.70532  0.69818  0.70
1      0.75         0.7        False           True  0.70196  0.67268  0.70
2      0.75         0.7        False           True  0.71243  0.69938  0.70
3      0.75         0.7        False           True  0.70226  0.69884  0.70
4      0.75         0.7        False           True  0.70292  0.69952  0.70
5      0.75         0.7         True           True  0.75100  0.69000  0.75
  •  Tags:  
  • Related