重新定义下列这段代码def get_tag(df): df = df['tag'].str.split('-') datalist = list(set(x for data in df for x in data)) return datalist df_tag = get_tag(df) print(df_tag) def get_lx(x,i): if i in str(x): return 1 else: return 0 for i in list(df_tag): df[i] = df['tag'].apply(get_lx,i=f'{i}') Series = df.iloc[:,7:].sum().sort_values(ascending=False) df_tag = [tag for tag in zip(Series.index.tolist(),Series.values.tolist())] df_iex = [index for index in Series.index.tolist()][:20] df_tag = [tag for tag in Series.values.tolist()][:20] df_tagiex = get_matplot(x=df_iex,y=df_tag,chart='plot',title='网易云音乐华语歌单标签图',size=10,ha='center',color=color[3]) print(df_tagiex)
时间: 2024-03-21 16:39:31 浏览: 41
好的,这里是重新定义后的代码:
```python
def get_tag(df):
df = df['tag'].str.split('-')
datalist = list(set(x for data in df for x in data))
return datalist
def get_lx(x, tag):
if tag in str(x):
return 1
else:
return 0
df_tag = get_tag(df)
print(df_tag)
for tag in df_tag:
df[tag] = df['tag'].apply(get_lx, tag=tag)
Series = df.iloc[:,7:].sum().sort_values(ascending=False)
df_tag = [tag for tag in zip(Series.index.tolist(),Series.values.tolist())]
df_iex = [index for index in Series.index.tolist()][:20]
df_tag = [tag for tag in Series.values.tolist()][:20]
df_tagiex = get_matplot(x=df_iex,y=df_tag,chart='plot',title='网易云音乐华语歌单标签图',size=10,ha='center',color=color[3])
print(df_tagiex)
```
这里的代码将get_lx函数中的参数i改为了tag,并使用了更加直观的命名方式。在for循环中,我们遍历每个标签,然后使用apply函数对每个标签进行处理,将其转化为0或1。最后,我们获取前20个标签进行可视化。
阅读全文