Determining when a column value changes in a pandas data frame

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

eg:

dataframe:

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

will tell me that the change happened between line 2 and line 3. I know how to use these values ​​for loops, but I was hoping there was a more pythonic approach to solving this problem.

+7
python pandas search csv dataframe
source share
1 answer

You can create a new column for the difference.

 > df['C'] = df['B'].diff() > print df # ABC 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 # ABC 2 3 4 4 1 

These will be your required lines

+10
source share

All Articles