I have the dataframe 'df1' contain 1226 rows × 13 columns I want to group it by 'Region' columns but it is not working
Asked
Active
Viewed 48 times
-3
-
2Please include any relevant information [as text directly into your question](https://stackoverflow.com/editing-help), do not link or embed external images of source code or data. Images make it difficult to efficiently assist you as they cannot be copied and offer poor usability as they cannot be searched. See: [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/q/285551/15497888) – Henry Ecker Aug 04 '21 at 05:00
-
1If you need assistance formatting a small sample of your DataFrame as a copyable piece of code for SO see [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/15497888). – Henry Ecker Aug 04 '21 at 05:00
-
It's unclear what are you trying to do. You've got a grouper object, but don't perform any grouping operation. Are you trying to create separate dataframes? Do you want the first 5 rows from every group is that what the `head` call is? Are you looking to sort the dataframe so regions appear together? What do you expect your code to be doing that it is not? – Henry Ecker Aug 04 '21 at 05:02
2 Answers
0
Try this out for grouping based on column
blockedGroup = df1.groupby('Region')
blocking_df = {}
for x in blockedGroup.groups:
temp_df = blockedGroup.get_group(x)
blocking_df.update({temp_df['customerProfile'].iloc[0]: temp_df})
This will return the groups into a dict structure, where keys will be the unique items in the region and the values will data frames.
e.g : {"USA": DataFrame}

Tamil Selvan
- 1,600
- 1
- 9
- 25
-
1not working bro. I figure out the solution that you have to use groupby function to reduce the rows combining it with another function (sum,count and etc...) in my case the code I wrote will not reduce the rows so , the function will not work ;alternatively I should use sort_values function. thank you for your help :D – Khalid Mohamed Aug 08 '21 at 05:03
0
df.groupby
does not change dataframe. Instead try doing it like this
df2 = df1.groupby('Region')
df2

Ambrish Pathak
- 3,813
- 2
- 15
- 30