简体   繁体   中英

Convert dataframe with tuples into dataframe with multiindex

I want to convert a dataframe which has tuples in cells into a dataframe with MultiIndex. Here is an example of the table code:

d = {2:[(0,2),(0,4)], 3:[(826.0, 826.0),(4132.0, 4132.0)], 4:[(6019.0, 6019.0),(12037.0, 12037.0)], 6:[(18337.0, 18605.0),(36674.0, 37209.0)]}
test = pd.DataFrame(d)

This is how the dataframe looks like:

        2                 3                   4                   6
0  (0, 2)    (826.0, 826.0)    (6019.0, 6019.0)  (18337.0, 18605.0)
1  (0, 4)  (4132.0, 4132.0)  (12037.0, 12037.0)  (36674.0, 37209.0)

This is what I want it to look like

     2       3        4        6
0 A  0   826.0   6019.0  18337.0
  B  2   826.0   6019.0  18605.0
1 A  0  4132.0  12037.0  36674.0
  B  4  4132.0  12037.0  37209.0

Thanks for your help!

Unsure for the efficiency, because this will rely an the apply method, but you could concat the dataframe with itself, adding a 'A' column to the first and a 'B' one to the second. Then you sort the resulting dataframe by its index, and use apply to change even rows to the first value of the tuple and odd ones to the second:

df = pd.concat([test.assign(X='A'), test.assign(X='B')]).set_index(
    'X', append=True).sort_index().rename_axis(index=(None, None))
df.iloc[0:len(df):2] = df.iloc[0:len(df):2].apply(lambda x: x.apply(lambda y: y[0]))
df.iloc[1:len(df):2] = df.iloc[1:len(df):2].apply(lambda x: x.apply(lambda y: y[1]))

It gives as expected:

     2     3      4      6
0 A  0   826   6019  18337
  B  2   826   6019  18605
1 A  0  4132  12037  36674
  B  4  4132  12037  37209

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