简体   繁体   中英

MultiIndex pivot table pandas python

import pandas as pd
data = pd.read_excel('.../data.xlsx')

the content looks like this:

Out[57]: 
        Block    Concentration           Name   Replicate           value
0         1            100           GlcNAc2          1               321
1         1            100           GlcNAc2          2               139
2         1            100           GlcNAc2          3               202
3         1             33           GlcNAc2          1                86
4         1             33           GlcNAc2          2               194
5         1             33           GlcNAc2          3               452
6         1             10           GlcNAc2          1               140
7         1             10           GlcNAc2          2               285

... ... ... ... ... ...

1742     24              0      Print buffer          1             -9968
1743     24              0      Print buffer          2             -4526
1744     24              0      Print buffer          3             14246

[1752 rows x 5 columns]

Pivot table looks like this (only a part of the large table):

 newdata = data.pivot_table(index=["Block", "Concentration"],columns=["Name","Replicate"], values="value")

在此处输入图片说明

my Questions:

how do i fill the '0' concentration of 'GlcNAc2' and 'Man5GIcNAc2' with the 'print buffer' values?

desired output:

在此处输入图片说明

i have been searching online and haven't really found anything similar. I have not even found a way to point to the 'print buffer' values from the 'Name' column.

from the MultiIndex/advanced indexing chapters it says to use

df.xs('one', level='second') 

but it doesn't work in my case, it doesn't work with pivot table, im not sure why , i'm confused. Is a pivot table multiindex??

If I understand correctly, you want to duplicate the values with Name == Print buffer to columns with Name == 'GlcNAc2' and 'Man5GIcNAc2' and concentration = 0 .

A way of doing this is to duplicate the rows in the original dataset :

selection = data[data["Name"] == "Print buffer"]'

selection.loc[:, "Name"] = "GlcNAc2"
data = pd.concat([data, selection])

selection.loc[:, "Name"] = "Man5GIcNAc2"
data = pd.concat([data, selection])

And then apply the pivot_table.

Remark : I am not sure that I understand your question. I am confused by the fact that in your pictures, the values Block == 1 change from the first picture to the second. Is it just a mistake or was that the core of your problem?

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