简体   繁体   中英

Pandas DataFrame pivot reset

I have a data frame and applying a pivot on that but the output is not as expected.

    import pandas as pd
    import numpy as np
    raw_data = {
            'PRODUCT': ['Display', 'InStream', 'Mobile'],
            'BLAZE_TAG_NAME_DESC': ['enewsletter', 'get_the_guide', 'logo'], 
            'CLICKTHRU': [19,30,40],
            'INTERACTION':[30,40,50]}
    df_a = pd.DataFrame(raw_data, columns = ['PRODUCT', 'BLAZE_TAG_NAME_DESC', 'CLICKTHRU','INTERACTION'])

    df_b = pd.pivot_table(df_a,index="PRODUCT",columns="BLAZE_TAG_NAME_DESC",values=['CLICKTHRU','INTERACTION'],aggfunc=np.sum,fill_value = 0)

Output:

在此处输入图片说明

I want the output is like

在此处输入图片说明

I tried the trick reset index. but that's not working

df.reset_index()

please help.

You can assign the columns names with None

df_b.columns.names=[None,None]
df_b
Out[54]: 
           CLICKTHRU                    INTERACTION                   
         enewsletter get_the_guide logo enewsletter get_the_guide logo
PRODUCT                                                               
Display           19             0    0          30             0    0
InStream           0            30    0           0            40    0
Mobile             0             0   40           0             0   50

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM