首页 > 解决方案 > 如何处理仅因为熊猫数据框中的大小写而延迟的值?

问题描述

我合并到类似的数据集,由于合并,我的许多值具有相同的值但不同的情况。我试图找到一种以最 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,另一个是小写。

标签: pythonpandasnumpy

解决方案


如果“修复”是指删除重复项,我建议在合并之前将两个数组都转换为小写。

这里有些例子:

>>> [x.lower() for x in ["A","B","C"]]
['a', 'b', 'c']

>>> df['x_lowercase'] = df['x'].str.lower()

推荐阅读