我有以下数据帧
code attribute rank_count
394 Feminine 9
394 Fresh 9
394 Heavy 8
418 Soft 13
418 Fresh 12
418 Clean 11
539 Fresh 14
539 Soft 14
539 Feminine 11
555 Feminine 9
555 Heavy 8
555 Soft 7
上面的dataframe有attributes字段和各种属性的代码字段,它们排在第三列,现在我需要它在下面的格式top1top2top3,应该是这样的
code top1 top2 top3
394 Feminine (9) Fresh (9) Heavy (8)
418 Soft(13) Fresh (12) Clean (11)
539 Fresh(14) soft(14) Feminine(11)
我无法使用pivot表的属性,因为属性很多,我正在尝试以top3的方式重新排列数据
您可以使用:
解释:
attribute
与rank_count
连接,通过^{3
DataFrame
编辑:
不含
assign
的溶液:这是使用
collections.defaultdict
的一种方法。你知道吗您可以根据需要更改列名并提供其他格式。在我看来,存储元组比将数字数据转换成字符串更好。你知道吗
如果您真的需要字符串表示…
您可以使用
pd.Series.apply
:相关问题 更多 >
编程相关推荐