How do you split a column into two different columns based on a criteria, but maintain one key? For example
col1 col2 time value
0 A sdf 16:00:00 100
1 B sdh 17:00:00 -40
2 A sf 18:00:45 300
3 D sfd 20:04:33 -89
I want a new dataframe like this
time main_val sub_val
0 16:00:00 100 NaN
1 17:00:00 NaN -40
2 18:00:45 300 NaN
3 20:04:33 NaN -89
You can use mask
:
mask = df['value'] < 0
df['main_val'] = df['value'].mask(mask)
df['sub_val'] = df['value'].mask(~mask)
df = df.drop(['col1','col2', 'value'], axis=1)
print (df)
time main_val sub_val
0 16:00:00 100.0 NaN
1 17:00:00 NaN -40.0
2 18:00:45 300.0 NaN
3 20:04:33 NaN -89.0
I use pd.get_dummies
, mask
, and mul
n = {True: 'main_val', False: 'sub_val'}
m = pd.get_dummies(df.value > 0).rename(columns=n)
df.drop('value', 1).join(m.mask(m == 0).mul(df.value, 0))
col1 col2 time sub_val main_val
0 A sdf 16:00:00 NaN 100.0
1 B sdh 17:00:00 -40.0 NaN
2 A sf 18:00:45 NaN 300.0
3 D sfd 20:04:33 -89.0 NaN
If you look at m.mask(m == 0)
, it becomes more clear how this works.
sub_val main_val
0 NaN 1.0
1 1.0 NaN
2 NaN 1.0
3 1.0 NaN
pd.get_dummies
gives us out zeros and ones. Then I make all the zeros into np.nan
. When I multiply with mul
, the df.value
column gets broadcast across both of these columns and we have our result. I use join
to attach it back to the dataframe.
We can improve the speed with numpy
v = df.value.values[:, None]
m = v > 0
n = np.where(np.hstack([m, ~m]), v, np.nan)
c = ['main_val', 'sub_val']
df.drop('value', 1).join(pd.DataFrame(n, df.index, c))
sub_val main_val
0 NaN 1.0
1 1.0 NaN
2 NaN 1.0
3 1.0 NaN
This Can even be Done By Pivot Table
df['Val1'] = np.where(df.value >=0,'main_val','sub_val' )
df = pd.pivot_table(df,index='time', values='value',
columns=['Val1'], aggfunc=np.sum).reset_index()
df = pd.DataFrame(df.values)
df.columns = ['time','main_val','sub_val']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With