我有数据帧a:

    TagID   Genre
0   0       rock
1   1       pop
2   2       favorites
3   3       alternative
4   4       love

和数据框b:

    Tags
0   154
1   20 35 40 65

我想要这样的结果:

  Genre
0 wjlb-fm
1 chill, rnb, loved, hip hop 

推荐答案

在加入第一个数据帧之前分解Tags列:

df2['Genre'] = (df2['Tags'].str.split().explode().astype(df1['TagID'].dtype)
                           .map(df1.set_index('TagID')['Genre'])
                           .groupby(level=0).agg(', '.join))
print(df2)

# Output
    Tags                 Genre
0      3           alternative
1  1 4 2  pop, love, favorites

一步一步地:

# 1. Explode your column
>>> out = df2['Tags'].str.split().explode().astype(df1['TagID'].dtype)
0    3
1    1
1    4
1    2
Name: Tags, dtype: int64

# 2. Match genre by tag id
>>> out = out.map(df1.set_index('TagID')['Genre'])
0    alternative
1            pop
1           love
1      favorites
Name: Tags, dtype: object

# 3. Reshape your dataframe
>>> out = out.groupby(level=0).agg(', '.join)
0             alternative
1    pop, love, favorites
Name: Tags, dtype: object

Python相关问答推荐

运行Python脚本时,用作命令行参数的SON文本

有症状地 destruct 了Python中的regex?

ODE集成中如何终止solve_ivp的无限运行

如何在Raspberry Pi上检测USB并使用Python访问它?

多处理队列在与Forking http.server一起使用时随机跳过项目

从嵌套的yaml创建一个嵌套字符串,后面跟着点

删除marplotlib条形图上的底边

我的字符串搜索算法的平均时间复杂度和最坏时间复杂度是多少?

CommandeError:模块numba没有属性generated_jit''''

Polars asof在下一个可用日期加入

如何在BeautifulSoup/CSS Select 器中处理regex?

如何杀死一个进程,我的Python可执行文件以sudo启动?

干燥化与列姆化的比较

如何删除重复的文字翻拍?

如何用FFT确定频变幅值

Django.core.exceptions.SynchronousOnlyOperation您不能从异步上下文中调用它-请使用线程或SYNC_TO_ASYNC

我可以同时更改多个图像吗?

如何批量训练样本大小为奇数的神经网络?

as_index=False groupBy不支持count

如何在函数签名中输入数据类字段