pandas 确定熊猫数据框中的列值何时发生变化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30196063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:20:02  来源:igfitidea点击:

Determining when a column value changes in pandas dataframe

pythoncsvsearchpandasdataframe

提问by badrobit

I am looking to write a quick script that will run through a csv file with two columns and provide me the rows in which the values in column B switch from one value to another:

我希望编写一个快速脚本,该脚本将运行包含两列的 csv 文件,并为我提供 B 列中的值从一个值切换到另一个值的行:

eg:

例如:

dataframe:

数据框:

# |  A  |  B  
--+-----+-----
1 |  2  |  3
2 |  3  |  3
3 |  4  |  4
4 |  5  |  4
5 |  5  |  4

would tell me that the change happened between row 2 and row 3. I know how to get these values using for loops but I was hoping there was a more pythonic way of approaching this problem.

会告诉我变化发生在第 2 行和第 3 行之间。我知道如何使用 for 循环获取这些值,但我希望有一种更 Pythonic 的方法来解决这个问题。

回答by Kathirmani Sukumar

You can create a new column for the difference

您可以为差异创建一个新列

> df['C'] = df['B'].diff()
> print df
   #  A  B   C
0  1  2  3 NaN
1  2  3  3   0
2  3  4  4   1
3  4  5  4   0
4  5  5  4   0

> df_filtered = df[df['C'] != 0]
> print df_filtered
   #  A  B  C
2  3  4  4  1

This will your required rows

这将是您所需的行