Home > OS >  Find the index value when the value changes in a column in dataframe - Pandas
Find the index value when the value changes in a column in dataframe - Pandas

Time:10-13

I have a dataframes as follows:

df1 =

     col_1   val_1
0    4.0     0.89
1    4.0     0.56
2    49.0    0.7
3    49.0    1.23
4    52.0    0.8
5    52.0    0.12
6    32.0    0.5

I want to find the index value when the value in col_1 changes and put in a list

I tried the following:

n_change = (np.where(~df1.col_1.diff( 1).isin([0, np.nan])))

But it returns a tuple of array and it is difficult to itterate through it.

I want a solution as follows

n_change = [2,4,6]

or 

n_change = array(2,4,6)

Is there a better way to do this?

CodePudding user response:

You can use:

df.index[df['col_1'].ne(df['col_1'].shift().bfill())]
# or with diff
# df.index[df['col_1'].diff().fillna(0).ne(0)]

output: Int64Index([2, 4, 6], dtype='int64')

As list:

df.index[df['col_1'].ne(df['col_1'].shift().bfill())].tolist()

output: [2, 4, 6]

With your solution:

np.where(~df.col_1.diff().isin([0, np.nan]))[0].tolist()

output: [2, 4, 6]

  • Related