python - 如何处理仅因为熊猫数据框中的大小写而延迟的值?
问题描述
我合并到类似的数据集,由于合并,我的许多值具有相同的值但不同的情况。我试图找到一种以最 Pythonic 的方式修复这些值的方法。
例如,我有一个武装专栏,它的独特价值是:
array([nan, 'Gun', 'Knife', 'Unarmed', 'Toy weapon', 'gun', 'unarmed',
'toy weapon', 'nail gun', 'knife', 'shovel', 'hammer', 'hatchet',
'undetermined', 'sword', 'machete', 'box cutter', 'metal object',
'screwdriver', 'lawn mower blade', 'flagpole',
'guns and explosives', 'cordless drill', 'crossbow', 'metal pole',
'Taser', 'metal pipe', 'metal hand tool', 'blunt object',
'metal stick', 'sharp object', 'meat cleaver', 'carjack',
"contractor's level", 'chain', 'unknown weapon', 'stapler',
'beer bottle', 'bean-bag gun', 'baseball bat and fireplace poker',
'straight edge razor', 'gun and knife', 'ax', 'brick',
'baseball bat', 'hand torch', 'chain saw', 'garden tool',
'scissors', 'pole', 'pick-axe', 'flashlight', 'baton', 'spear',
'chair', 'pitchfork', 'hatchet and gun', 'rock', 'piece of wood',
'bayonet', 'pipe', 'glass shard', 'motorcycle', 'pepper spray',
'metal rake', 'crowbar', 'oar', 'machete and gun', 'tire iron',
'air conditioner', 'pole and knife', 'baseball bat and bottle',
'fireworks', 'pen', 'chainsaw', 'gun and sword', 'gun and car',
'vehicle', 'pellet gun', 'claimed to be armed', 'BB gun',
'incendiary device', 'samurai sword', 'bow and arrow',
'gun and vehicle', 'vehicle and gun', 'wrench', 'walking stick',
'barstool', 'BB gun and vehicle', 'wasp spray', 'air pistol',
'Airsoft pistol', 'baseball bat and knife', 'vehicle and machete'],
dtype=object)
正如你所看到的,它们几乎被复制了,但一个是 Capital,另一个是小写。
解决方案
如果“修复”是指删除重复项,我建议在合并之前将两个数组都转换为小写。
这里有些例子:
>>> [x.lower() for x in ["A","B","C"]]
['a', 'b', 'c']
>>> df['x_lowercase'] = df['x'].str.lower()
推荐阅读
- c# - 一个视图的 2 个模型和转换不起作用
- python - `alembic revision --autogenerate` 产生冗余外键迁移
- java - 如何在java中订阅创建的对象中的属性
- ssis - SSIS 与目标 SQL Server 目标引发异常
- janusgraph - 如何在 janusgraph 中从 java/scala 客户端在服务器上创建图形
- antlr - ANTLR4 自动生成的充满错误的文件
- r - R add_row 在函数中使用时失败
- python - 无法使用 dateutil relativedelta 重构结束日期
- python - 熊猫:将具有相同键的行分组在一行中
- android - 如何从弹出的世博项目中制作独立的应用程序