python - How to group pandas dataframe by unique combination of two columns? -


i have pandas data frame below:

to       price ny    ca      2000 ny    mi      4000 ca    ny      3000 ny    ca      3000 

how can efficiently (and store) separate data frames each unique combination of , from? end goal make graphs using data frames formed. alternative(and more efficient) method welcome.

example:

df 1:

to       price ny    ca      2000 ny    ca      3000 

df 2:

to       price ny    mi      4000 

df 3:

to       price ca    ny      3000 

you can apply df.groupby operation on to , from , iterate on each group.

in [749]: df_list = [g _, g in df.groupby(['to', 'from'])]  in [750]: d in df_list:      ...:     print(d)      ...:     print('-' * 20)      ...:          price 2  ca   ny   3000 --------------------     price 0  ny   ca   2000 3  ny   ca   3000 --------------------     price 1  ny   mi   4000 -------------------- 

each element in df_list dataframe.


a word of advice not break these groups unless need to.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -