简体   繁体   中英

Python - difference iterating over columns

Just wanted a possible solution for iterating a difference over columns. Intuition says a simple for loop should be able to solve it, however, it's been a while and I am super rusty with my syntax etc. Or a group by with a lambda function?

Problem statement.

df looks like:

在此处输入图像描述

Obviously working with cohort files. I want to find the difference for col(n+1) - col(n) and iterate while grouping over the cohort group.

for example for 2014-04 my result should bedf 图像

This simple call should do the trick:

df.diff(-1, axis=1)

diff 's axis=1 will calculate a difference on the column axis. Check the doc here

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