【问题标题】:How to assign count of unique values to the records in a data frame in python如何将唯一值的计数分配给python中数据框中的记录
【发布时间】:2019-07-31 12:02:26
【问题描述】:

我有一个这样的数据框:

IP_address
   IP1
   IP1
   IP1
   IP4
   IP4
   IP4
   IP4
   IP4
   IP7
   IP7
   IP7

我想计算此列中的唯一值并将计数作为变量本身添加。最后应该是这样的:

IP_address  IP_address_Count
   IP1               3
   IP1               3
   IP1               3
   IP4               5
   IP4               5
   IP4               5
   IP4               5
   IP4               5
   IP7               3
   IP7               3
   IP7               3

我可以使用以下代码获取列的唯一值:

unique_ip_address_count = (df_c_train.drop_duplicates().IP_address.value_counts()).to_dict()

但是,我不确定如何在 python 的循环中匹配这些,以便我可以在 python 中获得所需的结果。非常感谢任何形式的帮助。

我无法在 stackoverflow 中找到等效的答案。如果有什么请指导我那里。谢谢你。

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    您可以将 value_counts() 与地图一起使用

    df['count'] = df['IP_address'].map(df['IP_address'].value_counts())
    
    
        IP_address  count
    0   IP1         3
    1   IP1         3
    2   IP1         3
    3   IP4         5
    4   IP4         5
    5   IP4         5
    6   IP4         5
    7   IP4         5
    8   IP7         3
    9   IP7         3
    10  IP7         3
    

    【讨论】:

    • 与我的相比,我更喜欢你的解决方案...... :)
    • @Vaishali - 我有一个问题。结果值是一个浮点数。我应该在这里做一些东西来转换成整数还是应该把它作为一个单独的代码?
    • 不应该。当我尝试 df.dtypes 时,我得到 IP_address 对象,count int64
    • 哦,好吧,我必须是 float64。
    • @Vaishali - float64 是因为内部数据问题。非常感谢您的帮助。
    【解决方案2】:

    使用pd.factorize
    这应该是一个非常快速的解决方案,可以很好地扩展到大数据

    f, u = pd.factorize(df.IP_address.values)
    df.assign(IP_address_Count=np.bincount(f)[f])
    
       IP_address  IP_address_Count
    0         IP1                 3
    1         IP1                 3
    2         IP1                 3
    3         IP4                 5
    4         IP4                 5
    5         IP4                 5
    6         IP4                 5
    7         IP4                 5
    8         IP7                 3
    9         IP7                 3
    10        IP7                 3
    

    【讨论】:

    • 是的,很快。 ..目前,我正在使用这种方法来计算唯一性;-)
    【解决方案3】:

    NumPy 方式 -

    tags, C = np.unique(df.IP_address, return_counts=1, return_inverse=1)[1:]
    df['IP_address_Count'] = C[tags]
    

    样本输出 -

    In [275]: df
    Out[275]: 
       IP_address  IP_address_Count
    0         IP1                 3
    1         IP1                 3
    2         IP1                 3
    3         IP4                 5
    4         IP4                 5
    5         IP4                 5
    6         IP4                 5
    7         IP4                 5
    8         IP7                 3
    9         IP7                 3
    10        IP7                 3
    

    【讨论】:

      【解决方案4】:
      In [75]: df['IP_address_Count'] = df.groupby('IP_address')['IP_address'].transform('size')
      
      In [76]: df
      Out[76]:
         IP_address  IP_address_Count
      0         IP1                 3
      1         IP1                 3
      2         IP1                 3
      3         IP4                 5
      4         IP4                 5
      5         IP4                 5
      6         IP4                 5
      7         IP4                 5
      8         IP7                 3
      9         IP7                 3
      10        IP7                 3
      

      【讨论】:

        【解决方案5】:
        ip_set = df.IP_address.unique()
        dict_temp = {}
        for ip in ip_set:
            dict_temp[ip] = df[df.IP_address == ip].IP_address.value_counts()[0]
        df['counts'] = [dict_temp[ip] for ip in df.IP_address]
        

        这似乎给了我你想要的那种输出

        编辑:Vaishali 对地图的使用非常完美

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-04-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-31
          • 2021-10-21
          • 2020-01-23
          相关资源
          最近更新 更多