Issue
I have a column with IDs and I divide the column into groups using pd.cut like this:
ID | Group |
---|---|
3645390 | 1 |
3678122 | 1 |
3615370 | 2 |
3371122 | 2 |
3645590 | 2 |
3778682 | 3 |
3125140 | 3 |
3578772 | 3 |
After that, how do I loop across the 'Group' column and pick out the IDs and assign them to an array.
array_1 = [3645390,3678122]
array_2 = [3615370,3371122,3645590]
.
so on..
Solution
It is often a bad practice to generate variables, the ideal is to use a container (dictionaries are ideal).
You could use groupby
and transform the output into dictionary of lists:
out = df.groupby('Group')['ID'].apply(list).to_dict()
Then access your lists by group key:
>>> out
{1: [3645390, 3678122],
2: [3615370, 3371122, 3645590],
3: [3778682, 3125140, 3578772]}
>>> out[1] ## group #1
[3645390, 3678122]
If you really want array_x
as keys:
(df.assign(Group='array_'+df['Group'].astype(str))
.groupby('Group')['ID'].apply(list).to_dict()
)
output:
{'array_1': [3645390, 3678122],
'array_2': [3615370, 3371122, 3645590],
'array_3': [3778682, 3125140, 3578772]}
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.