3

So I have a function replaceMonth(string), which is just a series of if statements that returns a string derived from a column in a pandas dataframe. Then I need to replace the original string with the derived one.

The dataframe is defined like this:

Index    ID        Year  DSFS           DrugCount
0        111111    Y1    3- 4 months    1

There are around 80K rows in the dataframe. What I need to do is to replace what is in column DSFS with the result from the replaceMonth(string) function.

So if, for example, the value in the first row of DSFS was '3-4 months', if I ran that string through replaceMonth() it would give me '_3_4' as the return value. Then I need to change the value in the dataframe from the '3- 4 months' to '_3_4'.

I've been trying to use apply on the dataframe but I'm either getting the syntax wrong or not understanding what it's doing correctly, like this:

dataframe['DSFS'].apply(replaceMonth(dataframe['DSFS']))

That doesn't ring right to me but I'm not sure where I'm messing up on it. I'm fairly new to Python so it's probably the syntax. :)

Any help is greatly appreciated!

1 Answer 1

6

When you apply you pass the function that you want applied to each element.

Try

dataframe['DSFS'].apply(replaceMonth)

Reassign to the dataframe to preserve the changes

dataframe['DSFS'] = dataframe['DSFS'].apply(replaceMonth)
Sign up to request clarification or add additional context in comments.

2 Comments

Darn, that's much more straightforward than I thought it would be. :) Worked perfectly! Thanks!
@piRSquared is there a way to do the same thing without the exact copy returned by apply()? Thanks in advance.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.