如何“分组”以将频率计数作为新列?

[我这样做了,但由于未按要求分组,所以没有得到预期的结果] [1]

df1 = pd.read_csv('https://cocl.us/sanfran_crime_dataset')
df1.drop(df1.columns.difference(['PdDistrict']),1,inplace=True)
df1 = df1.rename(columns={'PdDistrict':'Neighborhood'})
df1['Counts'] = df1.groupby('Neighborhood')['Neighborhood'].count() 
df1

Whats wrong with 4th line of code? groupby works fine if I do not assign it as a column but as I assign it as a column it doesn't even work.

评论