my input dataframe (abbreviated) is as follows:
>>> import numpy as np
>>> import pandas as pd
>>> df_in = pd.DataFrame([[1, 2, 'a', 3, 4], [6, 7, 'b', 8, 9]],
... columns=(['c1', 'c2', 'col', 'c3', 'c4']))
>>> df_in
c1 c2 col c3 c4
0 1 2 a 3 4
1 6 7 b 8 9
It is assumed that it will be controlled, i.e.
if the row (sample) in the column "col" (function) has a specific value (for example, "b" here) then convert the entries in columns "c1" and "c2" into one row in NumPy.NaNs.
Required Result:
>>> df_out = pd.DataFrame([[1, 2, 'a', 3, 4], [np.nan, np.nan, np.nan, 8, 9]],
columns=(['c1', 'c2', 'col', 'c3', 'c4']))
>>> df_out
c1 c2 col c3 c4
0 1 2 a 3 4
1 NaN NaN b 8 9
So far I have managed to get the desired result using code
>>> dic = {'col' : ['c1', 'c2']}
>>> b_w = df_in[df_in['col'] == 'b']
>>> b_w = b_w.drop(dic['col'], axis=1)
>>> b_wo = df_in[df_in['col'] != 'b']
>>> df_out = pd.concat([b_w, b_wo])
>>> df_out
c1 c2 c3 c4 col
1 NaN NaN 8 9 b
0 1.0 2.0 3 4 a
, ( ,
int, ),
. ,
pandas numpy, .
, ? .:)