How to use groupby and cumcount on unique names in a Pandas column

谁说胖子不能爱 提交于 2019-12-19 04:49:07

问题


I have a dataframe that looks like this

 ID ..... config_name    config_version  ...  
 aa           A                0         
 ab           A                7
 ad           A                7
 ad           A                27   
 bb           B                0     
 cc           C                0      
 cd           C                8 

I want to groupby config_name and apply cumcount on each unique config_version so that I get an additional column like

 ID ..... config_name    config_version     config_version_count 
 aa           A                0                     0        
 ab           A                7                     1
 ad           A                7                     1  
 ad           A                27                    2 
 bb           B                0                     0
 cc           C                0                     0
 cd           C                8                     1

But I can't seem to understand how to do it.

I tried using

      unique_count = df.groupby('config_name')['config_version'].cumcount()
      unique_count = pd.DataFrame({'config_name': [unique_count.index], 'config_version_count: [unique.count.values]})
      df = pd.merge(df,unique_count, on = 'config_name' , how = 'left')

Which gives the following output

 ID ..... config_name    config_version     config_version_count 
 aa           A                0                     0        
 ab           A                7                     1
 ad           A                7                     2  
 ad           A                27                    3 
 bb           B                0                     0
 cc           C                0                     0
 cd           C                8                     1

I also tried

 unique_count = df.drop_duplicates().groupby('config_name')['config_version'].cumcount()
  unique_count.reindex(df.index).ffill()
  df['config_version_count'] = unique_count

But this gives the same output as the first try.

Any idea how I could do this?


回答1:


Use CategoricalIndex with CategoricalIndex.codes:

df['config_version_count'] = (df.groupby('config_name')['config_version']
                                .transform(lambda x: pd.CategoricalIndex(x).codes))

print (df)
   ID config_name  config_version  config_version_count
0  aa           A               0                     0
1  ab           A               7                     1
2  ad           A               7                     1
3  ad           A              27                     2
4  bb           B               0                     0
5  cc           C               0                     0
6  cd           C               8                     1

Your solution should be working:

df['config_version_count'] = (df.drop_duplicates(['config_name','config_version'])
                                .groupby('config_name')
                                .cumcount())
df['config_version_count'] = df['config_version_count'].ffill().astype(int)



回答2:


Use pd.factorize()

df['config_version_count']=df.groupby('config_name')['config_version'].\
                                  transform(lambda x: pd.factorize(x)[0])
print(df)

   ID config_name  config_version  config_version_count
0  aa           A               0                     0
1  ab           A               7                     1
2  ad           A               7                     1
3  ad           A              27                     2
4  bb           B               0                     0
5  cc           C               0                     0
6  cd           C               8                     1


来源:https://stackoverflow.com/questions/54862986/how-to-use-groupby-and-cumcount-on-unique-names-in-a-pandas-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!