我想把一些列转换为adataframe中的列表。
Dataframe df:
Name salary department days other
0 ben 1000 A 90 abc
1 alex 3000 B 80 gf
2 linn 600 C 55 jgj
3 luke 5000 D 88 gg
所需输出df1:
Name list other
0 ben [1000,A,90] abc
1 alex [3000,B,80] gf
2 linn [600,C,55] jgj
3 luke [5000,D,88] gg
2条答案
按热度按时间00jrzges1#
如果你想保持这个顺序,那么我们可以把它分成3个部分,就像@mozway在他的回答中提到的那样
1.定义我们想要分组的列(如@mozway在他的回答中提到的)
1.查找第一个元素的索引(您可以向前一步查找最小的一个,因为列表不一定按照DataFrame排序)
1.将系列插入到 Dataframe 中我们生成的位置
结果是:
kkbh8khc2#
您可以将列切片并转换为list的list,然后转换为
Series
:输出: