如何分组并创建多索引数据框

时间:2020-06-30 21:00:35

标签: python pandas group-by

我有一个看起来像这样的数据框:

            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

我如何创建多索引数据框,该数据框将在groupby列中0。当我这样做时:

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

我无法将所有月份归为一类。

enter image description here

How to groupby and create a multiindex dataframe

1 个答案:

答案 0 :(得分:1)

根据您的信息,我建议以下内容:

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

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

请注意-您应将“月”更改为日期时间,否则顺序将不合逻辑。

另外,documentation对于熊猫来说非常强大。