Slartibartfast
Slartibartfast

Reputation: 1200

How to groupby and create a multiindex dataframe

I have a dataframe which looks like this:

            0         1              2
0       April  0.002745  ADANIPORTS.NS
1        July  0.005239  ASIANPAINT.NS
2       April  0.003347    AXISBANK.NS
3       April  0.004469  BAJAJ-AUTO.NS
4        June  0.006045  BAJFINANCE.NS
5        June  0.005176  BAJAJFINSV.NS
6       April  0.003321  BHARTIARTL.NS
7    November  0.003469    INFRATEL.NS
8       April  0.002667        BPCL.NS
9       April  0.003864   BRITANNIA.NS
10      April  0.005570       CIPLA.NS
11    October  0.000925   COALINDIA.NS
12      April  0.003666     DRREDDY.NS
13      April  0.002836   EICHERMOT.NS
14      April  0.003793        GAIL.NS
15      April  0.003850      GRASIM.NS
16      April  0.002858     HCLTECH.NS
17   December  0.005666        HDFC.NS
18      April  0.003484    HDFCBANK.NS
19      April  0.004173  HEROMOTOCO.NS
20      April  0.006395    HINDALCO.NS
21       June  0.001844  HINDUNILVR.NS
22    October  0.004620   ICICIBANK.NS
23      April  0.004020  INDUSINDBK.NS
24    January  0.002496        INFY.NS
25  September  0.001835         IOC.NS
26        May  0.002290         ITC.NS
27      April  0.005910    JSWSTEEL.NS
28      April  0.003570   KOTAKBANK.NS
29        May  0.003346          LT.NS
30      April  0.006131         M&M.NS
31      April  0.003912      MARUTI.NS
32      March  0.003596   NESTLEIND.NS
33      April  0.002180        NTPC.NS
34      April  0.003209        ONGC.NS
35       June  0.001796   POWERGRID.NS
36      April  0.004182    RELIANCE.NS
37      April  0.004246    SHREECEM.NS
38    October  0.004836        SBIN.NS
39      April  0.002596   SUNPHARMA.NS
40      April  0.004235         TCS.NS
41      April  0.006729  TATAMOTORS.NS
42    October  0.003395   TATASTEEL.NS
43     August  0.002440       TECHM.NS
44       June  0.003481       TITAN.NS
45      April  0.003749  ULTRACEMCO.NS
46      April  0.005854         UPL.NS
47      April  0.004991        VEDL.NS
48       July  0.001627       WIPRO.NS
49      April  0.003728        ZEEL.NS

how can i create a multiindex dataframe which would groupby in column 0. When i do:

new.groupby([0])
Out[315]: <pandas.core.groupby.generic.DataFrameGroupBy object at 0x0A938BB0>

I am not able to group all the months together.

enter image description here

How to groupby and create a multiindex dataframe

Upvotes: 0

Views: 54

Answers (1)

keg5038
keg5038

Reputation: 341

Based on your info, I'd suggest the following:

#rename columns to make useful 
new = new.rename(columns={0:'Month',1:'Price', 2:'Ticker'})

new.groupby(['Month','Ticker'])['Price'].sum()

To note - you should change change the 'Month' to a datetime or else the order will be illogical.

Also, the documentation is quite strong for pandas.

Upvotes: 1

Related Questions