3

I am trying to sort by the 'acct' and group by the 'month'.

dataframe is this:

    acct       month   value

14    84  2014-09-30  393641
15    84  2014-10-31  393283
16    84  2014-11-30  383293
....
35    85  2014-10-31  291629
36    85  2014-11-30  289544

I do a pivot table

df2 = df.pivot_table ( index = ['month'], columns = "acct" )

This is the results:

                value     
acct              84       85
month                    
2014-09-30  393641.0       0.0
2014-10-31  393283.0  291629.0
2014-11-30  383293.0  289544.0

What is the best way to get values from the pivot?
I need to loop trough the 'value' to extract the data created for each account.

Thanks.

This is what worked for me to extract the data:

var = [ ]
for column in df2.columns.values:
    var.append ( df2 [ column ].tolist () )
diogenes
  • 1,865
  • 3
  • 24
  • 51
  • You can use `df2 = df.pivot_table ( index = 'month', columns = "acct", values='value')` for remove `MultiIndex` from column. – jezrael Dec 15 '17 at 08:24
  • that makes is a simpler df, but how would I loop through it to just get the values. When I loop through it I get both the month and the values? thanks so much. – diogenes Dec 15 '17 at 08:30
  • this is what I am getting. month 2013-07-31 208372.0 2013-08-31 210669.0 2013-09-30 230171.0 – diogenes Dec 15 '17 at 08:31
  • Not sure if understand, what need? Select column? `df[85]` or `df['85']`? Or select column with index like `df.loc['2014-09-30',85]` ? – jezrael Dec 15 '17 at 08:33
  • looking for all the values from the df[85] and df[85] – diogenes Dec 15 '17 at 08:35
  • Yes, so `df[85]` does not work? – jezrael Dec 15 '17 at 08:46
  • yes, but it gives me both the month and the value. Plus how would I know it was the '85' in a loop? thanks for much for your help. – diogenes Dec 15 '17 at 09:39

0 Answers0