我目前有一个数据文件,它看起来像这样,我做了一个df["Keywords"] = df["Keywords"].str.split(" ")
ID | Keywords
1 | [agile]
2 | [python, python, python]
3 | [agile, agile]我想要实现的是这样的东西,在这种情况下,我将值转换为列标题并进行计数。我试过expand=true,但那不起作用。我能做到这一点吗?
ID | agile | python
1 | 1 | 0
2 | 0 | 1
3 | 1 | 0发布于 2022-10-03 16:23:13
不要split..。可以使用get_dummies直接对值进行编码。
df[['ID']].join(df['Keywords'].str.get_dummies(sep=' '))发布于 2022-10-03 17:09:53
这里有一条很长但不一样的路。首先,让我们在关键字列中查找每一行的关键字分布。
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}
'''然后将字典拆分为新列:
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
'''https://stackoverflow.com/questions/73937988
复制相似问题