首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将python列表转换为列标题并执行计数

将python列表转换为列标题并执行计数
EN

Stack Overflow用户
提问于 2022-10-03 16:03:13
回答 2查看 79关注 0票数 2

我目前有一个数据文件,它看起来像这样,我做了一个df["Keywords"] = df["Keywords"].str.split(" ")

代码语言:javascript
复制
ID | Keywords
1 | [agile]
2 | [python, python, python] 
3 | [agile, agile]

我想要实现的是这样的东西,在这种情况下,我将值转换为列标题并进行计数。我试过expand=true,但那不起作用。我能做到这一点吗?

代码语言:javascript
复制
ID | agile | python
1 | 1 | 0
2 | 0 | 1
3 | 1 | 0
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-10-03 16:23:13

不要split..。可以使用get_dummies直接对值进行编码。

代码语言:javascript
复制
df[['ID']].join(df['Keywords'].str.get_dummies(sep=' '))
票数 2
EN

Stack Overflow用户

发布于 2022-10-03 17:09:53

这里有一条很长但不一样的路。首先,让我们在关键字列中查找每一行的关键字分布。

代码语言:javascript
复制
def list_to_dict(row):
    freq = {}
    for item in row:
        if (item in freq):
            freq[item] += 1
        else:
            freq[item] = 1
 
    for key, value in freq.items():
        return freq

df['dicts']=df['keywords'].apply(lambda x: list_to_dict(x))
print(df)
'''
id  keywords                         dicts
1   ['agile']                        {'agile': 1}
2   ['python', 'python', 'python']   {'python': 3}
3   ['agile', 'agile']               {'agile': 2}
'''

然后将字典拆分为新列:

代码语言:javascript
复制
df = df.join(pd.json_normalize(df.pop('dicts'))).fillna(0)
float_cols=list(df.select_dtypes(include=['float']).columns)
for i in float_cols:
    df[i]=df[i].astype(int)
print(df)
'''
id                     keywords  agile  python
1                      [agile]   1      0
2     [python, python, python]   0      3
3               [agile, agile]   2      0
'''
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73937988

复制
相关文章

相似问题

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