seuadr
seuadr

Reputation: 143

python: pandas .describe - how to put results into a variable?

pretty new to python and pandas in general. I have a dataframe that has 2 columns i want to analyze:

df2 = df1.query('debt == 1').groupby(['family_status'])['family_status'].describe()

This gives me the result of:

                  count unique                top freq
family_status                                         
civil partnership   388      1  civil partnership  388
divorced             85      1           divorced   85
married             931      1            married  931
unmarried           274      1          unmarried  274
widow / widower      63      1    widow / widower   63

which is a lot of information that i wanted to know - however, to do some additional analysis i'd like to be able to put these results by 'family_status' into variables - so, civil partnership, divorced, married, etc. or feed them into an ad-hoc function. Edit for clarity - i want to have civil_partnership = the count (in this case 388) etc. unsure how to proceed.

thanks for your time in advance,

Jared

Upvotes: 2

Views: 924

Answers (2)

willwrighteng
willwrighteng

Reputation: 3031

It would be easy to create a dictionary and call each faimily_status as a key in that dict. Since you've assigned your describe dataframe to df2:

df2.reset_index(inplace=True, drop=False)

d = {}
for status, count in zip(df2['family_status'], df2['count']):
    d[status] = count

This should result in something like

d = {
'civil partnership'  : 388 
'divorced'           :  85 
'married'            : 931 
'unmarried'          : 274
'widow / widower'    :  63 
}

edit:

df2.count invokes the count method -- syntax adjusted so that it calls the column, not the method.

Upvotes: 1

wasif
wasif

Reputation: 15488

.describe() returns a normal dataframe, so you can assign it to amy variable and apply any dataframe methods in it.

And to select by index use .ix

Upvotes: 2

Related Questions