Iterating through rows of a Pandas series within a function

Jason

I'm writing a function part of which should iterate through the rows of a Series. The function should iterate through rows of a DataFrame column passed to it i.e. df['col'], however when I try to use .iterrows I get an error that a Series doesn't have that attribute and using .iteritems produces the error below. Are there any other ways to iterate through rows of a column? I need do be able to access the index and column value.

def get_RIMsin(df, obs, rimcol):
    """dataframe, obs column,rim column"""    
    maxval =df['Mmedian'].max()
    minval =df['Mmedian'].min()
    dfrange = maxval-minval
    amplitude = dfrange/2

    convert = (2*np.pi)/365
    startday = obs.idxmax().dayofyear
    sinmax = 91

    for row in rimcol.iteritems: #This is where I'd like to go through rows of a series
        diff = sinmax - startday
        adjday = row.dayofyear + diff
        adjsin = np.sin(adjday * convert)
        df['RIMsine'] = row + adjsin
    return df

get_RIMsin(sve_DOC, sve_DOC['DOC_mg/L'], sve_DOC['RIMsDOC'])

TypeError                                 Traceback (most recent call last)
<ipython-input-98-4811cbf80e78> in <module>()
     17     return df
     18 
---> 19 get_RIMsin(sve_DOC, sve_DOC['DOC_mg/L'], sve_DOC['RIMsDOC'])
     20 """get_RIM2(svv_DOC, svv_DOC['DOC_mg/L'], svv_DOC['RIMsDOC'])
     21 get_RIM2(svw_DOC, svw_DOC['DOC_filt_mg/l'], svw_DOC['RIMsDOC'])

<ipython-input-98-4811cbf80e78> in get_RIMsin(df, obs, rimcol)
     10     sinmax = 91
     11 
---> 12     for row in rimcol.iteritems:
     13         diff = sinmax - startday
     14         adjday = row.dayofyear + diff

TypeError: 'instancemethod' object is not iterable
Dan Allan

None of this actually required row iteration, as Ryan G pointed out in the comments. I think this (untested!) code is equivalent.

convert = (2*np.pi)/365
sinmax = 91

def get_RIMsin(df, obs, rimcol):
    """dataframe, obs column,rim column"""    
    amplitude = df['Mmedian'].ptp()/2

    startday = obs.idxmax().dayofyear

    diff = sinmax - startday
    adjday = rimcol + diff
    adjsin = np.sin(adjday * convert)
    df['RIMsine'] = adjsin
    return df

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

pandas if statements iterating through rows

From Dev

error iterating through rows in pandas

From Dev

Iterating Through Rows of Pandas DataFrame - change series value based on values in another series?

From Dev

Iterating through selected columns and rows in Pandas

From Dev

Iterating through populated rows

From Dev

Removing rows from a pandas dataframe while iterating through it

From Dev

Drop rows while iterating through groups in pandas groupby

From Dev

Iterating Through Multiple Rows and Outputting the Result Inside a Function

From Dev

Iterating within groups in Pandas

From Dev

Iterating Over Every Item in a Series in Pandas With A Custom Function

From Dev

Iterating through function names

From Dev

Iterating through function names

From Dev

How can I avoid counting triplicates as pairs while iterating through an ordered series of letters within an array?

From Dev

How can I avoid counting triplicates as pairs while iterating through an ordered series of letters within an array?

From Dev

Iterating through rows where rows could be "missing"

From Dev

Iterating through pandas groupby groups

From Dev

Iterating through list of Dataframes Pandas

From Dev

Python/Pandas Iterating through columns

From Dev

Iterating through pandas groupby groups

From Dev

Iterating through multiple dataframes pandas

From Dev

SQL Server iterating through time series data

From Dev

Efficient way of iterating rows in pandas

From Dev

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

From Dev

Iterating through list within Razor View

From Dev

Altering a global var within function - more specifically a pandas series

From Dev

Iterating through c array through function

From Dev

Iterating through rows to capture the value in the next row

From Dev

C# Interop - Iterating through rows and deleting

From Dev

Iterating through rows of SQLite table to update ids

Related Related

  1. 1

    pandas if statements iterating through rows

  2. 2

    error iterating through rows in pandas

  3. 3

    Iterating Through Rows of Pandas DataFrame - change series value based on values in another series?

  4. 4

    Iterating through selected columns and rows in Pandas

  5. 5

    Iterating through populated rows

  6. 6

    Removing rows from a pandas dataframe while iterating through it

  7. 7

    Drop rows while iterating through groups in pandas groupby

  8. 8

    Iterating Through Multiple Rows and Outputting the Result Inside a Function

  9. 9

    Iterating within groups in Pandas

  10. 10

    Iterating Over Every Item in a Series in Pandas With A Custom Function

  11. 11

    Iterating through function names

  12. 12

    Iterating through function names

  13. 13

    How can I avoid counting triplicates as pairs while iterating through an ordered series of letters within an array?

  14. 14

    How can I avoid counting triplicates as pairs while iterating through an ordered series of letters within an array?

  15. 15

    Iterating through rows where rows could be "missing"

  16. 16

    Iterating through pandas groupby groups

  17. 17

    Iterating through list of Dataframes Pandas

  18. 18

    Python/Pandas Iterating through columns

  19. 19

    Iterating through pandas groupby groups

  20. 20

    Iterating through multiple dataframes pandas

  21. 21

    SQL Server iterating through time series data

  22. 22

    Efficient way of iterating rows in pandas

  23. 23

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

  24. 24

    Iterating through list within Razor View

  25. 25

    Altering a global var within function - more specifically a pandas series

  26. 26

    Iterating through c array through function

  27. 27

    Iterating through rows to capture the value in the next row

  28. 28

    C# Interop - Iterating through rows and deleting

  29. 29

    Iterating through rows of SQLite table to update ids

HotTag

Archive