Apply function with args in pandas

WoodChopper

I am trying to find whether Date falls in PromoInterval in a data frame.

print dset1

        Store   Date    PromoInterval
1760    2   2013-05-04  Jan,Apr,Jul,Oct
1761    2   2013-05-03  Jan,Apr,Jul,Oct
1762    2   2013-05-02  Jan,Apr,Jul,Oct
1763    2   2013-05-01  Jan,Apr,Jul,Oct
1764    2   2013-04-30  Jan,Apr,Jul,Oct

def func(a,b):
    y = b.split(",")
    z = {1:'Jan',2:'Feb',3:'Mar', 4:'Apr',5:'May',6:'Jun',7:'Jul',8:'Aug',9:'Sep',
        10:'Oct',11:'Nov',12:'Dec'}
    return (z[a] in y)

dset1.apply(func, axis=1, args = (dset1['Date'].dt.month, dset1['PromoInterval']) )

Struck at below error:

dset1.apply(func, axis=1, args = (dset1['Date'].dt.month, >dset1['PromoInterval']) ) ('func() takes exactly 2 arguments (3 given)', u'occurred at index 1760')

Data set:

{'Date': {1760: Timestamp('2013-05-04 00:00:00'),
  1761: Timestamp('2013-05-03 00:00:00'),
  1762: Timestamp('2013-05-02 00:00:00'),
  1763: Timestamp('2013-05-01 00:00:00'),
  1764: Timestamp('2013-04-30 00:00:00')},
 'PromoInterval': {1760: 'Jan,Apr,Jul,Oct',
  1761: 'Jan,Apr,Jul,Oct',
  1762: 'Jan,Apr,Jul,Oct',
  1763: 'Jan,Apr,Jul,Oct',
  1764: 'Jan,Apr,Jul,Oct'},
 'Store': {1760: 2, 1761: 2, 1762: 2, 1763: 2, 1764: 2}}
firelynx

I would start by formatting the text string of the month using a lambda function on the 'Date' column:

df['Month'] = df['Date'].apply(lambda x: x.strftime('%b'))

Then I would fire a lambda function on axis=1 which means it operates on the x axis over the dataframe. Here I simply check if 'Month' is in 'PromoInterval'

df[['PromoInterval', 'Month']].apply(lambda x: x[1] in x[0], axis=1)

1760    False
1761    False
1762    False
1763    False
1764     True
dtype: bool

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Apply function to multiple pandas columns with Args

From Dev

Alternative to apply function in pandas

From Dev

Exception Handling in Pandas .apply() function

From Java

Apply ewm function on Pandas groupby

From Java

apply using a function with iloc pandas

From Dev

Speeding up Pandas apply function

From Dev

Pandas Groupby Apply Function to Level

From Dev

pandas apply function with arguments no lambda

From Dev

Counting within Pandas apply() function

From Dev

Apply Customize Cumulative Function to Pandas

From Dev

Counting within Pandas apply() function

From Dev

Pandas and apply function to match a string

From Dev

Writing function using *args to create a new column in pandas dataframe

From Dev

Optimizing pandas filter inside apply function

From Java

getting the index of a row in a pandas apply function

From Dev

Apply function on each column in a pandas dataframe

From Dev

How to apply scipy function on Pandas data frame

From Dev

pandas apply function to multiple columns and multiple rows

From Dev

Using 'apply' in Pandas (externally defined function)

From Dev

pandas DataFrame, how to apply function to a specific column?

From Dev

Pandas Apply lambda function null values

From Dev

Python pandas apply function if a column value is not NULL

From Dev

pandas - apply UTM function to dataframe columns

From Dev

pandas find max value in groupby and apply function

From Dev

Apply function over relative rows in Pandas

From Dev

Apply function on cumulative values of pandas series

From Dev

Apply custom cumulative function to pandas dataframe

From Dev

How to apply a function ( BigramCollocationFinder) to Pandas DataFrame

From Dev

What is the difference between pandas agg and apply function?

Related Related

  1. 1

    Apply function to multiple pandas columns with Args

  2. 2

    Alternative to apply function in pandas

  3. 3

    Exception Handling in Pandas .apply() function

  4. 4

    Apply ewm function on Pandas groupby

  5. 5

    apply using a function with iloc pandas

  6. 6

    Speeding up Pandas apply function

  7. 7

    Pandas Groupby Apply Function to Level

  8. 8

    pandas apply function with arguments no lambda

  9. 9

    Counting within Pandas apply() function

  10. 10

    Apply Customize Cumulative Function to Pandas

  11. 11

    Counting within Pandas apply() function

  12. 12

    Pandas and apply function to match a string

  13. 13

    Writing function using *args to create a new column in pandas dataframe

  14. 14

    Optimizing pandas filter inside apply function

  15. 15

    getting the index of a row in a pandas apply function

  16. 16

    Apply function on each column in a pandas dataframe

  17. 17

    How to apply scipy function on Pandas data frame

  18. 18

    pandas apply function to multiple columns and multiple rows

  19. 19

    Using 'apply' in Pandas (externally defined function)

  20. 20

    pandas DataFrame, how to apply function to a specific column?

  21. 21

    Pandas Apply lambda function null values

  22. 22

    Python pandas apply function if a column value is not NULL

  23. 23

    pandas - apply UTM function to dataframe columns

  24. 24

    pandas find max value in groupby and apply function

  25. 25

    Apply function over relative rows in Pandas

  26. 26

    Apply function on cumulative values of pandas series

  27. 27

    Apply custom cumulative function to pandas dataframe

  28. 28

    How to apply a function ( BigramCollocationFinder) to Pandas DataFrame

  29. 29

    What is the difference between pandas agg and apply function?

HotTag

Archive