根据其他列值/Pandas -Python 在数据框中创建 ID 列 [英] create ID column in dataframe based on other column values / Pandas -Python

查看:105
本文介绍了根据其他列值/Pandas -Python 在数据框中创建 ID 列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的数据框

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|

我已经使用代码创建了 C_N 标签

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)

现在我想要另一列,我也可以在其中获取特定组的 ID,我的理想输出是:

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!

推荐答案

我已经定义了一个自定义函数来检索所需的数据:

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)

输出df

这篇关于根据其他列值/Pandas -Python 在数据框中创建 ID 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆