简体   繁体   中英

How to rewrite Pandas frame.append with concat

I have the following code which works perfectly putting in subtotals and grand totals. With the frame.append method deprecated how should this be rewritten?

pvt = pd.concat([y.append(y.sum()
                           .rename((x, 'Total')))
                for x, y in table.groupby(level=0)
                 ]).append(table.sum()
                                 .rename(('Grand', 'Total')))

Use

pvt = pd.concat([y for x, y in table.groupby(level=0)] + \
                [y.sum().rename((x, 'Total')) for x, y in table.groupby(level=0)] + \
                [table.sum().rename(('Grand', 'Total'))])

# or

pvt = pd.concat([x for _, y in table.groupby(level=0) for x in (y, y.sum().rename((x, 'Total')))] + \
                [table.sum().rename(('Grand', 'Total'))])

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