Id 1 2 3 4
z1 3 0 2 2
z1 1 4 4 3
z2 8 1 7 9
z2 0 0 2 3
z2 5 6 7 9
z3 0 5 6 2
z3 4 4 8 2
Here is my data, I want to group every column into lists by Id, result should be this
Id 1 2 3 4
z1 [3,1] [0,4] [2,4] [2,3]
z2 [8,0,5][1,0,6][7,2,7][9,3,9]
z3 [0,4] [5,4] [6,8] [2,2]
So here is the thing I could do every column separately but I've done that and now I need to optimize this, is there any way for this to be done once for every column?? If not, maybe there is a way that works faster than pandas.groupby??
First I think working with list
s in pandas is not good idea .
But if really need it, it is possible by DataFrameGroupBy.agg
with list
:
df = df.groupby('Id').agg(list)
print (df)
1 2 3 4
Id
z1 [3, 1] [0, 4] [2, 4] [2, 3]
z2 [8, 0, 5] [1, 0, 6] [7, 2, 7] [9, 3, 9]
z3 [0, 4] [5, 4] [6, 8] [2, 2]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.