Python 相对于其他列中的值,将值指定给新列是否重复
我的数据框架是这样的,基本上是每月用户访问一个站点:Python 相对于其他列中的值,将值指定给新列是否重复,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我的数据框架是这样的,基本上是每月用户访问一个站点: month user_id 1 1 1 2 1 1 1 3 2 1 2 2 2 4 3 2 3 5 3 1 我想创建一个包含0或1的列。每个用户_id将只获得一次1,其他时间将获得0 期望输出示例 我认为您需要设置为0列user\u id的重复值: df['new'] = (~df.duplicated('user_id')).astype
month user_id
1 1
1 2
1 1
1 3
2 1
2 2
2 4
3 2
3 5
3 1
我想创建一个包含0或1的列。每个用户_id将只获得一次1,其他时间将获得0
期望输出示例
我认为您需要设置为
0
列user\u id
的重复值:
df['new'] = (~df.duplicated('user_id')).astype(int)
或:
我认为您需要设置为
0
列user\u id
的重复值:
df['new'] = (~df.duplicated('user_id')).astype(int)
或:
下面是另一段使用基本操作的代码:
i=0
df['new']=""
#a new empty column
for a in range(len(df)):
if(df.iloc[a,1]>i):
#get a th index entry for user_id(1)
df.iloc[a,2]=1
#set value to one
#a,2 means ath entry for 3 column (new)
i+=1
else:
df.iloc[a,2]=0
下面是另一段使用基本操作的代码:
i=0
df['new']=""
#a new empty column
for a in range(len(df)):
if(df.iloc[a,1]>i):
#get a th index entry for user_id(1)
df.iloc[a,2]=1
#set value to one
#a,2 means ath entry for 3 column (new)
i+=1
else:
df.iloc[a,2]=0
我看不出结果是如何按月统计用户的。他们只是标记新用户,而不考虑月份。我看不出结果是如何按月统计用户的。他们只是简单地标记新用户,而不考虑月份。
i=0
df['new']=""
#a new empty column
for a in range(len(df)):
if(df.iloc[a,1]>i):
#get a th index entry for user_id(1)
df.iloc[a,2]=1
#set value to one
#a,2 means ath entry for 3 column (new)
i+=1
else:
df.iloc[a,2]=0