and I want to add a last column, count, containing the number of fsq belonging to the digits group, i.e: fsq digits digits_type count0 1 1 odd 31 2 1 odd 32 3 1 odd 33 11 2 even 24 22 2 even 25 101 3 odd 26 111 3 odd 2因为有3个 fsq 行的 digits 等于1,所以有2个 fsq 行具有 digits 等于2,等等.Since there are 3 fsq rows that has digits equal to 1, 2 fsq rows that has digits equal to 2, etc.推荐答案 In [395]: df['count'] = df.groupby('digits')['fsq'].transform(len)In [396]: dfOut[396]: fsq digits digits_type count0 1 1 odd 31 2 1 odd 32 3 1 odd 33 11 2 even 24 22 2 even 25 101 3 odd 26 111 3 odd 2[7 rows x 4 columns] 这篇关于DataFrame:添加具有组大小的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-13 23:41