Reputation: 568
I have a dataframe like this
L_1 D_1 L_2 D_2 L_3 D_3 C_N
1 Boy Boy||
1 Boy 1-1 play Boy|play|
1 Boy 1-1 play 1-1-21 car Boy|play|car
1 Boy 1-1 play 1-1-1 online Boy|play|online
2 Girl Girl||
2 Girl 2-1 dance Girl|dance|
I have created the C_N
tab using the code
df['C_N'] = df[['D_1','D_2', 'D_3']].apply(lambda x: '|'.join(x), axis=1)
Now I would like another column where I can also get the IDs of particular group, my ideal output would be :
L_1 D_1 L_2 D_2 L_3 D_3 IDs C_N
1 Boy 1 Boy||
1 Boy 1-1 play 1-1 Boy|play|
1 Boy 1-1 play 1-1-21 car 1-1-21 Boy|play|car
1 Boy 1-1 play 1-1-1 online 1-1-1 Boy|play|online
2 Girl 2 Girl||
2 Girl 2-1 dance 2-1 Girl|dance|
can anyone help me in this issue. Thank you in advance!
Upvotes: 2
Views: 1023
Reputation: 342
I have defined a custom function to retrieve the required data:
df = pd.DataFrame([
['1', 'Boy','','','',''],
['1', 'Boy','1-1','play','',''],
['1', 'Boy','1-1','play','1-1-21','car'],
['1', 'Boy','1-1','play','1-1-1','online'],
['2', 'Girl','','','',''],
['2', 'Girl','','dance','','']], columns=['L_1','D_1','L_2','D_2','L_3','D_3']
)
df['C_N'] = df[['D_1','D_2', 'D_3']].apply(lambda x: '|'.join(x), axis=1)
def get_data(x,y,z):
result = []
if x != '':
result.append(x)
if y != '':
result.append(y)
if z != '':
result.append(z)
return result[-1]
df['IDs'] = ''
df['IDs'] = df.apply(lambda row: get_data(row['L_1'], row['L_2'], row['L_3']), axis=1)
Output df
Upvotes: 2
Reputation: 18315
df = df.replace("^\s*$", np.nan, regex=True)
id_inds = df.filter(like="L_").agg(pd.Series.last_valid_index, axis=1)
# either this (but deprecated..)
df["IDs"] = df.lookup(df.index, id_inds)
# or this
df["IDs"] = df.to_numpy()[np.arange(len(df)), df.columns.get_indexer(id_inds)]
First we replace empty cells with NaN
and then look at the L_*
columns. Getting their last_valid_index
es which gives column names. Then we can either lookup
(deprecated), or go to numpy values and do fancy indexing with get_indexer
,
to get
>>> df
L_1 D_1 L_2 D_2 L_3 D_3 C_N IDs
0 1 Boy NaN NaN NaN NaN Boy|| 1
1 1 Boy 1-1 play NaN NaN Boy|play| 1-1
2 1 Boy 1-1 play 1-1-21 car Boy|play|car 1-1-21
3 1 Boy 1-1 play 1-1-1 online Boy|play|online 1-1-1
4 2 Girl NaN NaN NaN NaN Girl|| 2
5 2 Girl 2-1 dance NaN NaN Girl|dance| 2-1
You can now replace the NaN
s back with empty string, if you wish.
Upvotes: 1