0

I have a dataframe with multiple columns. I need to flag 4th value to 8th value in Column2 indicating these consecutive rows have values equal to 0.1

ID           Column2      Column3
1              2.6            9
2              1.9            7
3              4.7            5
4              0.1            8     
5              0.1            6
6              0.1            2
7              0.1            8
8              0.1            7
9              3.0            4
10             1.2            9
11             2.8            7
12             3.1            9
13             1.0            3
14             2.4            4
15             3.2            5
n             nth value      nth value

I used following code and it gave me TypeError. Can anyone please help?

Code:

import pandas as pd
df = pd.read_csv('file.csv')

for row, index in df_May.iterrows():
    if row['Column2'] == row['Column2'].shift(1) | row['Column2'] == 0.1:
        print index,row

TypeError: 'int' object has no attribute '__getitem__'
1
  • how do you want to flag those rows? please post your desired data set Commented Jun 12, 2018 at 19:16

1 Answer 1

4

You need:

df.loc[(df['Column2'].diff()==0) | (df['Column2'].diff(-1)==0)]

Output:

    ID  Column2 Column3
3   4   0.1       8
4   5   0.1       6
5   6   0.1       2
6   7   0.1       8
7   8   0.1       7
Sign up to request clarification or add additional context in comments.

2 Comments

I think add or .diff(-1) == 0 to also catch the first occurrence.
@HarvIpan that worked like magic! Thanks for making it so simple. ALollz, thanks for your comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.