简体   繁体   中英

Pandas pivot_table

I am trying to create a pivot table that has the indexes listed in each row. Currently, I have them only appearing in the first instance.

df = pd.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo",
                         "bar", "bar", "bar", "bar"],
                   "B": ["one", "one", "one", "two", "two",
                         "one", "one", "two", "two"],
                   "C": ["small", "large", "large", "small",
                         "small", "large", "small", "small",
                         "large"],
                   "D": [1, 2, 2, 3, 3, 4, 5, 6, 7],
                   "E": [2, 4, 5, 5, 6, 6, 8, 9, 9]})
df
     A    B      C  D  E
0  foo  one  small  1  2
1  foo  one  large  2  4
2  foo  one  large  2  5
3  foo  two  small  3  5
4  foo  two  small  3  6
5  bar  one  large  4  6
6  bar  one  small  5  8
7  bar  two  small  6  9
8  bar  two  large  7  9
table = pd.pivot_table(df, values='D', index=['A', 'B'],
                    columns=['C'], aggfunc=np.sum)
table
C        large  small
A   B
bar one    4.0    5.0
    two    7.0    6.0
foo one    4.0    1.0
    two    NaN    6.0

I would like for foo and bar to appear in the 2nd and 4th lines respectively so that all rows have a values.

This is documented in docs : 在此处输入图像描述

So you can do:

with pd.option_context('display.multi_sparse', False):
    print(table)

C        large  small
A   B                
bar one    4.0    5.0
bar two    7.0    6.0
foo one    4.0    1.0
foo two    NaN    6.0

This is a by-product of the display mechanism for MultiIndex data. Use table.reset_index() to move the index back into columns, and you will see all the columns being displayed with all values.

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