This is my pandas dataframe I am referring to.
基本上我希望能够显示 'crime type'
的计数基于'council'
。例如,其中 'council == Fermanagh and omagh'
数数
每个 'crime type'
都有不同的值这有道理吗?所以burgulary
可能等于 1
而,Anti-social behaviour'
将是3
另一个'council'
?然后我想将这些值绘制在条形图上。
希望这是有道理的。任何帮助都会很棒。谢谢。
最佳答案
df1 = df.groupby(['crime type', 'council']).size().reset_index(name='Count')
编辑:
df = pd.DataFrame({'crime type':['Anti-social behaviour','Anti-social behaviour',
'Burglary','Burglary'],
'council':['Fermanagh and omagh','Belfast','Belfast','Belfast']})
df1 = df.groupby(['council', 'crime type']).size().unstack(fill_value=0)
print (df1)
crime type Anti-social behaviour Burglary
council
Belfast 1 2
Fermanagh and omagh 1 0
df1.plot.bar()
关于Python Pandas : How can I count the number of times a value appears in a column based upon another column?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47620346/