我已经用 Python 创建了一个 DataFrame,并希望显示每个邮政编码最流行的狗品种。我已经编码了以下内容,但我只能显示每个品种的总数,而不是品种本身。我的代码:import pandas as pddf = pd.DataFrame({'zip_code':[12345,66666,12345,22222,22222,12345,66666,22222,44444], 'primary_breed': ['labrador','pug','poodle','labrador','labrador','pug','whippet','poodle','labrador'], 'animals_name':['lucy','charley','scout','hank','sweetie','lucy','daddy','lucy','charley'], 'species':['dog','dog','dog','dog','dog','dog','dog','dog','dog']})# assign correct data typesdf['species'] = df['species'].astype('category')df['animals_name'] = df['animals_name'].astype('string')df['primary_breed'] = df['primary_breed'].astype('category')df['zip_code'] = df['zip_code'].astype('string')dogs = df.species == 'dog'# total number per breed per zipdf_total_per_breed_zip = df[dogs].groupby('zip_code')['primary_breed'].value_counts() print('\n\ntotal number per breed: \n', df_total_per_breed_zip)# most popular breed per zipdf_mostpop_breed_zip = df_total_per_breed_zip.max(level='zip_code')print('\n\nmost popular breed per zip: \n', df_mostpop_breed_zip)所以我得到的是:total number per breed: zip_code primary_breed12345 labrador 1 poodle 1 pug 122222 labrador 2 poodle 144444 labrador 166666 pug 1 whippet 1Name: primary_breed, dtype: int64most popular breed per zip: zip_code12345 122222 244444 166666 1Name: primary_breed, dtype: int64但我想得到的是:total number per breed: zip_code primary_breed12345 labrador 1 poodle 1 pug 122222 labrador 2 poodle 144444 labrador 166666 pug 1 whippet 1Name: primary_breed, dtype: int64most popular breed per zip: zip_code12345 labrador22222 labrador44444 labrador66666 pugName: primary_breed, dtype: int64
1 回答
杨魅力
TA贡献1811条经验 获得超6个赞
用于mode最常见的:
(df.loc[df['species']=='dog']
.groupby('zip_code')['primary_breed']
.agg(lambda x: x.mode()[0])
)
输出:
zip_code
12345 labrador
22222 labrador
44444 labrador
66666 pug
Name: primary_breed, dtype: object
添加回答
举报
0/150
提交
取消