首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从列中具有重复值和非重复值的数据帧中创建列表

从列中具有重复值和非重复值的数据帧中创建列表
EN

Stack Overflow用户
提问于 2022-04-29 09:05:24
回答 1查看 49关注 0票数 0

我有这样的数据框架

代码语言:javascript
复制
data = [['Ma', 1,'too'], ['Ma', 1,'taa'], ['Ma', 1,'tuu',],['Ga', 2,'too'], ['Ga', 2,'taa'], ['Ga', 2,'tuu',]]
 

df = pd.DataFrame(data, columns = ['NAME', 'AID','SUBTYPE'])
 

NAME ID SUBTYPE
Ma    1     too
Ma    1     taa
Ma    1     tuu
Ga    2     too
Ga    2     taa
Ga    2     tuu

有重复的名称和ID以及不同的子类型

我想要一个这样的名单

代码语言:javascript
复制
Ma-1-[too,taa,too],Ga-2-[too,taa,tuu]

编辑:名称和ID应该总是相同的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-29 09:45:18

通常,为了在Python中实现这一点,我们将使用字典,因为键不能被复制。

代码语言:javascript
复制
# We combine the NAME and ID keys, so we can use them together as a key.
df["NAMEID"] = df["NAME"] + "-" + df["ID"].astype(str)

# Convert the desired fields to lists.
name_id_list = df["NAMEID"].tolist()
subtype_list = df["SUBTYPE"].tolist()

# Loop through the lists by zipping them together.
results_dict = {}
for name_id, subttype in zip(name_id_list, subtype_list):
    if results_dict.get(name_id):
        # If the key already exists then instead we append them to the end of the list. 
        results_dict[name_id].append(subttype)
    else:
        # If key not exists add them as key-value pairs to a dictionary.
        results_dict[name_id] = [subtype]

结果dict最终会看起来如下:

代码语言:javascript
复制
{'Ma-1': ['too', 'taa', 'tuu'], 'Ga-2': ['too', 'taa', 'tuu']}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72055466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档