Applying iterative function to every group in pandas DataFrame

ymoiseev

I have large pandas DataFrame with following format:

        prod_id     timestamp     text
150523  0006641040  9.393408e+08  text_1 
150500  0006641040  9.408096e+08  text_2 
150499  0006641041  1.009325e+09  text_3 
150508  0006641041  1.018397e+09  text_4 
150524  0006641042  1.025482e+09  text_5

DataFrame is sorted by prod_id and timestamp. What I am trying to do, is to enumerate a counter for every prod_id based on the timestamp from earliest to latest. For example, I am trying to achieve something like this:

        prod_id     timestamp     text    enum  
150523  0006641040  9.393408e+08  text_1  1
150500  0006641040  9.408096e+08  text_2  2 
150499  0006641041  1.009325e+09  text_3  1 
150508  0006641041  1.018397e+09  text_4  2 
150524  0006641042  1.025482e+09  text_5  1

I can do this iteratively quite easily by going through each row and increasing counter, but is there a way to do this in a more functional programming fashion?

MaxU

UPDATE:

In [324]: df
Out[324]:
        prod_id     timestamp    text
150523  6641040  9.393408e+08  text_1
150500  6641040  9.408096e+08  text_2
150501  6641040  9.408096e+08  text_3
150499  6641041  1.009325e+09  text_3
150508  6641041  1.018397e+09  text_4
150524  6641042  1.025482e+09  text_5

In [325]: df['enum'] = df.groupby(['prod_id'])['timestamp'].cumcount() + 1

In [326]: df
Out[326]:
        prod_id     timestamp    text  enum
150523  6641040  9.393408e+08  text_1     1
150500  6641040  9.408096e+08  text_2     2
150501  6641040  9.408096e+08  text_3     3
150499  6641041  1.009325e+09  text_3     1
150508  6641041  1.018397e+09  text_4     2
150524  6641042  1.025482e+09  text_5     1

OLD answer:

In [314]: df['enum'] = df.groupby(['prod_id'])['timestamp'].rank().astype(int)

In [315]: df
Out[315]:
        prod_id     timestamp    text  enum
150523  6641040  9.393408e+08  text_1     1
150500  6641040  9.408096e+08  text_2     2
150499  6641041  1.009325e+09  text_3     1
150508  6641041  1.018397e+09  text_4     2
150524  6641042  1.025482e+09  text_5     1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Applying iterative function to every group in pandas DataFrame

From Dev

Applying function to every other column in pandas dataframe

From Dev

Applying a function to pandas dataframe

From Dev

Applying function to Pandas dataframe by column

From Dev

Applying function to Pandas dataframe by column

From Dev

applying a lambda function to pandas dataframe

From Dev

R: Applying a function to every element of a dataframe

From Dev

R: Applying a function to every entry in dataframe

From Dev

Pandas - Applying Function to every other row

From Dev

Pandas: Applying a function to each group independently

From Java

Applying a custom aggregation function to a pandas DataFrame

From Dev

Applying a function to a MultiIndex pandas.DataFrame column

From Dev

How to subset a Pandas dataframe applying a function by date?

From Dev

Applying a function to every combination of two columns in a dataframe using R

From Dev

Applying a function to every combination of two columns in a dataframe using R

From Dev

Applying function to every cell in a Dataframe based on index and col

From Java

applying function to each group using dplyr and return specified dataframe

From Dev

applying regex to a pandas dataframe

From Dev

pandas applying multicolumnindex to dataframe

From Dev

Applying re to Pandas Dataframe

From Dev

Accessing columns after applying the stack function on a pandas dataframe

From Dev

Applying function to columns of a Pandas DataFrame, conditional on data type

From Dev

Accessing columns after applying the stack function on a pandas dataframe

From Dev

Ho to get/return a single dictionary by applying function on pandas dataframe

From Dev

Applying function to dataframe column

From Dev

R applying function on a dataframe

From Dev

Applying a function to a pandas col

From Dev

Applying a function to a pandas col

From Dev

Applying a function on every 3 columns (cols: 1-3, 4-6, 7-9) in dataframe

Related Related

  1. 1

    Applying iterative function to every group in pandas DataFrame

  2. 2

    Applying function to every other column in pandas dataframe

  3. 3

    Applying a function to pandas dataframe

  4. 4

    Applying function to Pandas dataframe by column

  5. 5

    Applying function to Pandas dataframe by column

  6. 6

    applying a lambda function to pandas dataframe

  7. 7

    R: Applying a function to every element of a dataframe

  8. 8

    R: Applying a function to every entry in dataframe

  9. 9

    Pandas - Applying Function to every other row

  10. 10

    Pandas: Applying a function to each group independently

  11. 11

    Applying a custom aggregation function to a pandas DataFrame

  12. 12

    Applying a function to a MultiIndex pandas.DataFrame column

  13. 13

    How to subset a Pandas dataframe applying a function by date?

  14. 14

    Applying a function to every combination of two columns in a dataframe using R

  15. 15

    Applying a function to every combination of two columns in a dataframe using R

  16. 16

    Applying function to every cell in a Dataframe based on index and col

  17. 17

    applying function to each group using dplyr and return specified dataframe

  18. 18

    applying regex to a pandas dataframe

  19. 19

    pandas applying multicolumnindex to dataframe

  20. 20

    Applying re to Pandas Dataframe

  21. 21

    Accessing columns after applying the stack function on a pandas dataframe

  22. 22

    Applying function to columns of a Pandas DataFrame, conditional on data type

  23. 23

    Accessing columns after applying the stack function on a pandas dataframe

  24. 24

    Ho to get/return a single dictionary by applying function on pandas dataframe

  25. 25

    Applying function to dataframe column

  26. 26

    R applying function on a dataframe

  27. 27

    Applying a function to a pandas col

  28. 28

    Applying a function to a pandas col

  29. 29

    Applying a function on every 3 columns (cols: 1-3, 4-6, 7-9) in dataframe

HotTag

Archive