Iterate over rows in a pandas dataframe and apply a lambda function

Venkatesh

I have a pandas dataframe that has multiple columns, of which I am interested in a specific column that has a series of (1, or 0). The logic that I want to perform is:

If (the current row is 1 and the next row is 0):
    count = count + 1
else :
    pass
df['NewCol'] = count

so, this is what I tried:

secCnt = 0 
def sectionCount(data):
    global secCnt
    if( (data[['secFlg']] == 0) and (data[['secFlg'].shift(-1)] == 1) ):
        secCnt = secCnt + 1 
    else:
        pass
    return secCnt


if __name__ == "__main__":
    df['SectionIndex'] = df.apply(sectionCount(df), axis=1)

I get the error:

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

pI am new to pandas and am performing text extraction from a pdf file and am interested in finding out sections in the pdf file

jezrael

I think need create boolean mask with comparing by 0 chaning by & (bitwise AND) with shifted values and for count use cumsum:

np.random.seed(1213)

df = pd.DataFrame({'secFlg':np.random.randint(2, size=20)})

df['SectionIndex'] = ((df['secFlg'] == 0) & (df['secFlg'].shift() == 1)).cumsum()
print (df)
    secFlg  SectionIndex
0        0             0
1        1             0
2        1             0
3        1             0
4        0             1
5        0             1
6        0             1
7        0             1
8        0             1
9        1             1
10       0             2
11       0             2
12       0             2
13       0             2
14       1             2
15       1             2
16       1             2
17       0             3
18       1             3
19       0             4

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to iterate over rows using apply to a dataframe?

From Java

How to iterate over rows in a DataFrame in Pandas

From Dev

Iterate over rows and expand pandas dataframe

From Dev

Iterate over pandas dataframe rows as pure text

From Dev

Apply function over relative rows in Pandas

From Dev

Apply function over rows in pandas df

From Dev

Julia iterate over rows of dataframe

From Dev

Iterate over groups of rows in Pandas

From Dev

Apply a match and replace function over series of rows in a dataframe in order

From Dev

Iterate over rows in a Dataframe and compare it to the rest of the rows

From Dev

pandas apply function that returns multiple values to rows in pandas dataframe

From Dev

Iterate over column in dataframe (Pandas)

From Dev

Using a shift() function within an apply function to compare rows in a Pandas Dataframe

From Dev

Use function (not using lambda) with apply method for pandas DataFrame

From Dev

Apply function to pandas dataframe row using values in other rows

From Dev

iterate over Dataframe rows using values

From Dev

Iterate over first N rows in pandas

From Dev

Pandas iterate over rows and conditional count

From Java

How iterate over rows in a dataframe dictionnary and change some values - Pandas Python

From Dev

pandas apply function with arguments no lambda

From Dev

Iterate over pandas dataframe in jinja2

From Dev

How to iterate the rows of a DataFrame as Series in Pandas?

From Dev

using apply and lambda to iterate through a dataframe collecting values

From Dev

Trouble passing in lambda to apply for pandas DataFrame

From Dev

What is wrong with this pandas dataframe.apply(lambda)?

From Dev

applying a lambda function to pandas dataframe

From Dev

Pandas dataframe apply calculations to selected rows

From Dev

Iterate over all possible combinations of columns and rows in a dataframe

From Dev

List Comprehension Over Pandas Dataframe Rows

Related Related

  1. 1

    How to iterate over rows using apply to a dataframe?

  2. 2

    How to iterate over rows in a DataFrame in Pandas

  3. 3

    Iterate over rows and expand pandas dataframe

  4. 4

    Iterate over pandas dataframe rows as pure text

  5. 5

    Apply function over relative rows in Pandas

  6. 6

    Apply function over rows in pandas df

  7. 7

    Julia iterate over rows of dataframe

  8. 8

    Iterate over groups of rows in Pandas

  9. 9

    Apply a match and replace function over series of rows in a dataframe in order

  10. 10

    Iterate over rows in a Dataframe and compare it to the rest of the rows

  11. 11

    pandas apply function that returns multiple values to rows in pandas dataframe

  12. 12

    Iterate over column in dataframe (Pandas)

  13. 13

    Using a shift() function within an apply function to compare rows in a Pandas Dataframe

  14. 14

    Use function (not using lambda) with apply method for pandas DataFrame

  15. 15

    Apply function to pandas dataframe row using values in other rows

  16. 16

    iterate over Dataframe rows using values

  17. 17

    Iterate over first N rows in pandas

  18. 18

    Pandas iterate over rows and conditional count

  19. 19

    How iterate over rows in a dataframe dictionnary and change some values - Pandas Python

  20. 20

    pandas apply function with arguments no lambda

  21. 21

    Iterate over pandas dataframe in jinja2

  22. 22

    How to iterate the rows of a DataFrame as Series in Pandas?

  23. 23

    using apply and lambda to iterate through a dataframe collecting values

  24. 24

    Trouble passing in lambda to apply for pandas DataFrame

  25. 25

    What is wrong with this pandas dataframe.apply(lambda)?

  26. 26

    applying a lambda function to pandas dataframe

  27. 27

    Pandas dataframe apply calculations to selected rows

  28. 28

    Iterate over all possible combinations of columns and rows in a dataframe

  29. 29

    List Comprehension Over Pandas Dataframe Rows

HotTag

Archive