I have one dataframe -
ID T Q1 P Q2
10 xy 1 pq 2
20 yz 1 rs 1
20 ab 1 tu 2
30 cd 2 cu 2
30 xy 1 mu 1
30 bb 1 bc 1
Now I need a dictionary with Id as key and rest of the column values as a list to be the dictionary's value
output :
{10:['xy',1,'pq',2]}
{20:['ab',1,'tu',2]}
{30:['bb',1,'bc',1]}
Expected result :
{10:[['xy',1,'pq',2]]}
{20:[['yz',1,'rs',1],['ab',1,'tu',2]]}
{30:[['cd',2,'cu',2],['xy',1,'mu',1],['bb',1,'bc',1]]}
Try:
x = (
df.groupby("ID")
.apply(lambda x: x.iloc[:, 1:].agg(list).values.tolist())
.to_dict()
)
print(x)
Prints:
{10: [['xy', 1, 'pq', 2]],
20: [['yz', 1, 'rs', 1], ['ab', 1, 'tu', 2]],
30: [['cd', 2, 'cu', 2], ['xy', 1, 'mu', 1], ['bb', 1, 'bc', 1]]}
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.