简体   繁体   English

Python - 迭代列的差异

[英]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?直觉说一个简单的 for 循环应该能够解决它,但是,已经有一段时间了,我对我的语法等非常生疏。或者与 lambda function 组?

Problem statement.问题陈述。

df looks like: df 看起来像:

在此处输入图像描述

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.我想找到 col(n+1) - col(n) 的区别,并在对队列组进行分组时进行迭代。

for example for 2014-04 my result should be例如对于 2014-04 我的结果应该是df 图像

This simple call should do the trick:这个简单的调用应该可以解决问题:

df.diff(-1, axis=1)

diff 's axis=1 will calculate a difference on the column axis. diffaxis=1将计算列轴上的差异。 Check the doc here此处查看文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM