pythonpandas重复数据统计_python – 计算Pandas DataFrame中的重复值

必须有一个简单的方法来做到这一点,但我无法在SO上找到一个优雅的解决方案或自己解决.

我正在尝试根据DataFrame中的列集计算重复值的数量.

例:

print df

Month LSOA code Longitude Latitude Crime type

0 2015-01 E01000916 -0.106453 51.518207 Bicycle theft

1 2015-01 E01000914 -0.111497 51.518226 Burglary

2 2015-01 E01000914 -0.111497 51.518226 Burglary

3 2015-01 E01000914 -0.111497 51.518226 Other theft

4 2015-01 E01000914 -0.113767 51.517372 Theft from the person

我的解决方法:

counts = dict()

for i, row in df.iterrows():

key = (

row['Longitude'],

row['Latitude'],

row['Crime type']

)

if counts.has_key(key):

counts[key] = counts[key] + 1

else:

counts[key] = 1

而我得到的数字:

{(-0.11376700000000001, 51.517371999999995, 'Theft from the person'): 1,

(-0.111497, 51.518226, 'Burglary'): 2,

(-0.111497, 51.518226, 'Other theft'): 1,

(-0.10645299999999999, 51.518207000000004, 'Bicycle theft'): 1}

除了这个代码也可以改进(随意评论如何),通过pandas做什么的方法?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值