python - 在Python中 reshape 数据框

标签 python pandas dataframe

我有一个音乐数据集。其中有两列( 'Music_id' 和 'Tag_name' )。 示例数据:

Music_id       Tag_name
  1004         Base Guitar
  1004         Guitar
  2004         Base guitar
  1005         Keyboard
  1003         Keyboard
  1004         Keyboard

我想 reshape 此数据框,其中所有 tag_name 都应成为列名称,以便每一行都应具有唯一的 Music_id

我读到了分层和合并的概念,但不知道如何在这种情况下应用。有什么帮助吗?

最佳答案

问题不清楚。也许给我们您期望的输出

我猜你需要这个?

pd.crosstab(df.Music_id, df.Tag_name, dropna=False)

enter image description here

关于python - 在Python中 reshape 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61807785/

相关文章:

python - 如何在 Pytorch 中创建上三角矩阵?

python - 用python画动态图

python - Opencv 和 python 中的 K 均值聚类 : Is there any option to cluster in mahalanobis distance?

python - 按照 Pandas 中 3 列的笛卡尔积组织行

python - 以矢量化方式将列添加到 pandas DataFrame 中以其他列值为条件

Python/ Pandas /日期时间 : transform entire lists in a column to datetime

python - 选择多列时 pandas 中的 keyError

python - 如何根据 pandas 中的给定键更改背景单元格颜色?

python - 按时差阈值匹配数据帧

python - 标签不在列表中和 KeyError