Home > Mobile >  Python Pandas How to combine columns according to the condition of checking another column
Python Pandas How to combine columns according to the condition of checking another column

Time:09-01

I have a DataFrame:

value,combined,value_shifted,Sequence_shifted,long,short
12834.0,2.0,12836.0,3.0,2.0,-2.0
12813.0,-2.0,12781.0,-3.0,-32.0,32.0
12830.0,2.0,12831.0,3.0,1.0,-1.0
12809.0,-2.0,12803.0,-3.0,-6.0,6.0
12822.0,2.0,12805.0,3.0,-17.0,17.0
12800.0,-2.0,12807.0,-3.0,7.0,-7.0
12773.0,2.0,12772.0,3.0,-1.0,1.0
12786.0,-2.0,12787.0,1.0,1.0,-1.0
12790.0,2.0,12784.0,3.0,-6.0,6.0

I want to combine the long and short columns according to the value of the combined column

If df.combined == 2 then we leave the value long

If df.combined == -2 then we leave the value short

Expected result:

value,combined,value_shifted,Sequence_shifted,calc
12834.0,2.0,12836.0,3.0,2.0
12813.0,-2.0,12781.0,-3.0,32
12830.0,2.0,12831.0,3.0,1.0
12809.0,-2.0,12803.0,-3.0,6.0
12822.0,2.0,12805.0,3.0,-17
12800.0,-2.0,12807.0,-3.0,-1.0
12773.0,2.0,12772.0,3.0,-1.0
12786.0,-2.0,12787.0,1.0,-6.0
12790.0,2.0,12784.0,3.0,20.0

CodePudding user response:

Use if possible 2,-2 or another values in combined column numpy.select:

df['calc'] = np.select([df['combined'].eq(2), df['combined'].eq(-2)],
                       [df['long'], df['short']])

Or if only 2,-1 values use numpy.where:

df['calc'] = np.where(df['combined'].eq(2), df['long'], df['short'])

CodePudding user response:

Try this:

df['calc'] = df['long'].where(df['combined'] == 2, df['short'])

CodePudding user response:

df['calc'] = np.nan

mask_2 = df['combined'] == 2
df.loc[mask_2, 'calc'] = df.loc[mask_2, 'long']

mask_minus_2 = df['combined'] == -2
df.loc[mask_minus_2, 'calc'] = df.loc[mask_minus_2, 'short']

then you can drop the long and short columns:

df.drop(columns=['long', 'short'], inplace=True)
  • Related