简体   繁体   中英

From unstack() to dataframe in Pandas

Hello, I am trying to create a new dataframe from unstack() of an original dataframe.

  • My original dataframe (df) is the following:

     idx = [np.array(['Jan', 'Jan', 'Feb', 'Mar', 'Mar', 'Mar']),np.array(['A1', 'A2', 'A2', 'A1', 'A3', 'A4'])] data = [{'x': 1, 'y': 50}, {'x': 5, 'y': 40}, {'x': 3, 'y': 20}, {'x': 2, 'y': 70}, {'x': 7, 'y': 10}, {'x': 3, 'y': 80}] df = pd.DataFrame(data, index=idx, columns=['x','y']) df.index.names=['date','type'] 

And it looks like this:

           x   y
date type
Jan  A1    1  50
     A2    5  40
Feb  A2    3  20
Mar  A1    2  70
     A3    7  10
     A4    3  80
  • My goal is to create a new dataframe (df2) that looks like this:

      xA1 xA2 xA3 xA4 yA1 yA2 yA3 yA4 Jan 1 5 Nan Nan 50 40 Nan Nan Feb Nan 3 Nan Nan Nan 20 Nan Nan Mar 2 Nan 7 3 70 Nan 10 80 

** I have tried the following code:

df2 = df.unstack()

This gets me very close, but I don't know how to move from here to the dataframe that I want.

Thank you

So you can do unstack , then flatten the multiple index

s=df.unstack()
s.columns=s.columns.map(''.join)
s
Out[70]: 
      xA1  xA2  xA3  xA4   yA1   yA2   yA3   yA4
date                                            
Feb   NaN  3.0  NaN  NaN   NaN  20.0   NaN   NaN
Jan   1.0  5.0  NaN  NaN  50.0  40.0   NaN   NaN
Mar   2.0  NaN  7.0  3.0  70.0   NaN  10.0  80.0

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