我是Python / Pandas的初学者,目前正在使用IPython笔记本开展一些项目。我遇到了一个小问题,我无法通过我的书或谷歌搜索解决,可能是因为我不确定要搜索的术语或功能。
我们说我有一个带行的DataFrame
Industry Category
Software/Industry Systems Software/Medical Systems Software/Payment Electronic Components Database Applications Online Communities Medical Equipment Mobile Phones
我想要的是创建一个新行,用于分配"行业类别"到父母类别"。在这个例子中只是"软件"和"硬件"。
Industry Category Parent Category
Software/Industry Systems Software Software/Medical Systems Software Software/Payment Software Electronic Components Hardware Database Applications Software Online Communities Software Medical Equipment Hardware Mobile Phones Hardware
注意:我的列表中有大约600个行业类别项目,我需要对其中的大约30个类别进行排序。
如果有一些选项可以使用带有两行的* .csv来完成这项工作,那将会很棒。在左边所有"行业类别"项目和右边所需的"父母类别"我想申请数据集。
谢谢!
答案 0 :(得分:1)
我这么做了很多。我会创建一个字典并使用apply
和lambda
。
example_dict = {'Software/Industry Systems':'Software','Software/Payment':'Software'}
dataframe['Parent Category'] = dataframe['Industry Category'].apply(lambda value: example_dict[value])