Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 相对于其他列中的值,将值指定给新列是否重复_Python_Python 3.x_Pandas_Dataframe - Fatal编程技术网

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