2

I have the following dataframe

df = pd.DataFrame({'col_1': ['Animal','Deer','Sheep','Fish','Vehicle','Truck','Car','Mineral','Gold','Silver'], 
                   'col_2': ['Animal',0.5,0.25,0.25,'Vehicle',0.5,0.5,'Mineral',0.75,0.25],
                   'Group': [0,0,0,0,1,1,1,2,2,2]})

I want to create a dictionary of series. I want column 1 to be the index, column 2 the value and Group to specify the series. EG the name (key) for the first series would be "Animal" and it should look like:
enter image description here

I've tried the following. But it's not right, I'm getting a dictionary of dataframes instead of series and the headers are in the first row.

dict_of_series = {i: df.loc[df.group == i, ['col_1', 'col_2']] for i in range(1, df.group.iat[-1])} 
1
  • 2
    Can you put your expected output
    – Tbaki
    Dec 9, 2021 at 13:25

1 Answer 1

1

Use dictionary comprhension for loop by groupby object with DataFrame.set_axis for set columnsnames by first row per groups, remove first row and last column by indexing in DataFrame.iloc and last remove columns names in DataFrame.rename_axis :

dict_of_series = {g['col_2'].iat[0]: 
                  g.set_axis(g.iloc[0], axis=1).iloc[1:, :-1].rename_axis(None, axis=1) 
                  for i, g in df.groupby('Group')} 


print (dict_of_series['Animal'])
  Animal Animal
1   Deer    0.5
2  Sheep   0.25
3   Fish   0.25

print (dict_of_series['Vehicle'])
  Vehicle Vehicle
5   Truck     0.5
6     Car     0.5

print (dict_of_series['Mineral'])
  Mineral Mineral
8    Gold    0.75
9  Silver    0.25

For Series use DataFrame.set_index before solution and also change iloc for select last column to Series and last Series.rename_axis:

df = df.set_index('col_1')

dict_of_series = {g['col_2'].iat[0]: 
                  g.set_axis(g.iloc[0], axis=1).iloc[1:, 0].rename_axis(None)
                  for i, g in df.groupby('Group')} 


print (dict_of_series['Animal'])
Deer      0.5
Sheep    0.25
Fish     0.25
Name: Animal, dtype: object
5
  • works perfect, can you help me understand the dictionary comprehension? For example, what does "i, g in df.groupby('Group')" do? How does python know what is i and what is g? and I'm use to putting an aggregator at the end of the groupby. Dec 9, 2021 at 16:26
  • This is a dictionary of dataframes and I'm looking for a dictionary of series. How do I move the first column over to the index within the comprehension? Dec 9, 2021 at 19:02
  • @JonathanHay - You are right, description was missing. Added to answer also for generate Series like need.
    – jezrael
    Dec 10, 2021 at 9:42
  • How would this be modified to handle an additional series that only had one element (eg Beef is turned into an integer and lost)? For example df = pd.DataFrame({'col_1': ['Animal','Deer','Sheep','Fish','Vehicle','Truck','Car','Mineral','Gold','Silver','Meat',Beef], 'col_2': ['Animal',0.5,0.25,0.25,'Vehicle',0.5,0.5,'Mineral',0.75,0.25,'Meat',1], 'Group': [0,0,0,0,1,1,1,2,2,2,3,3]}) Apr 29, 2022 at 13:25
  • @JonathanHay - I have to go away, sorry. Can you post new question with link to this solution? I have full weekend, so in Monday can check it.
    – jezrael
    Apr 29, 2022 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.