使用pandas根据其他列中的条件创建一个新的ID列 [英] Create a new ID column based on conditions in other column using pandas

查看:117
本文介绍了使用pandas根据其他列中的条件创建一个新的ID列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个新列"ID",该列在每次数据"列中没有"NaN"值时都应提供唯一的ID.如果非null值彼此正确,则ID保持不变.为了更好地理解,我提供了我的最终Id列应如何如下所示.有人可以指导我吗?

I am trying to make a new column 'ID' which should give a unique ID each time there is no 'NaN' value in 'Data' column. If the non null values come right to each other, the ID remains the same. I have provided how my final Id column should look like below as reference to better understand. Could anyone guide me on this?

Id  Data
0   NaN
0   NaN
0   NaN
1   54
1   55
0   NaN
0   NaN
2   67
0   NaN
0   NaN
3   33
3   44
3   22
0   NaN

推荐答案

使用factorize

v=pd.factorize(df.Data.isnull().cumsum()[df.Data.notnull()])[0]+1
df.loc[df.Data.notnull(),'Newid']=v
df.Newid.fillna(0,inplace=True)
df
    Id  Data  Newid
0    0   NaN    0.0
1    0   NaN    0.0
2    0   NaN    0.0
3    1  54.0    1.0
4    1  55.0    1.0
5    0   NaN    0.0
6    0   NaN    0.0
7    2  67.0    2.0
8    0   NaN    0.0
9    0   NaN    0.0
10   3  33.0    3.0
11   3  44.0    3.0
12   3  22.0    3.0
13   0   NaN    0.0

这篇关于使用pandas根据其他列中的条件创建一个新的ID列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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