Determining when a column value changes in pandas dataframe

后端 未结 2 1992
面向向阳花
面向向阳花 2020-12-09 09:29

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 anoth

相关标签:
2条回答
  • 2020-12-09 10:08

    You can do the following which also works for non numerical values:

    >>> import pandas as pd
    >>> df = pd.DataFrame({"Status": ["A","A","B","B","C","C","C"]})
    >>> df["isStatusChanged"] = df["Status"].shift(1, fill_value=df["Status"].head(1)) != df["Status"]
    >>> df
      Status  isStatusChanged
    0      A            False
    1      A            False
    2      B             True
    3      B            False
    4      C             True
    5      C            False
    6      C            False
    >>> 
    
    

    Note the fill_value could be different depending on your application.

    0 讨论(0)
  • 2020-12-09 10:11

    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

    0 讨论(0)
提交回复
热议问题