Reputation: 73
I have an df which has this structure:
paramter names ids
0 0.008 t 1
1 0.349 P1 1
2 0.120 P2 1
3 0.008 t 2
4 0.349 P1 2
5 0.120 P2 2
6 0.209 t 3
7 0.349 P1 3
8 0.200 P2 3
9 0.209 t 4
10 0.349 P1 4
11 0.200 P2 4
... ... ... ...
3000
I want to extract for each element, with given id, the parameters of P1, P2, t. If all of the parameters of one element are the same as for another element, then I want to add them to the same list. Here 1,2 would be in the same list and 3,4 in another list, because all three values of 1 correspondent to 2 and all three values of 3 correspondent to 4.
I do not know how many different elements I have, so I do not know how may lists I need.
Upvotes: 4
Views: 680
Reputation: 71689
Use pivot
to reshape the dataframe with index as id
, columns as names
and values as parameter
, then use groupby
on P1, p2, t
and collect ids belonging to same group in list comprehension:
df1 = df.pivot('ids', 'names', 'paramter').reset_index()
ids = [g['ids'].tolist() for _, g in df1.groupby(['P1', 'P2', 't'])]
Details:
print(df1)
names ids P1 P2 t
0 1 0.349 0.12 0.008
1 2 0.349 0.12 0.008
2 3 0.349 0.20 0.209
3 4 0.349 0.20 0.209
print(ids)
[[1, 2], [3, 4]]
Upvotes: 4