Home > Mobile >  Python create a function with a chain within
Python create a function with a chain within

Time:04-14

I come from R and we love pipes, it makes really easier to understand the code and I try to apply it as mucha s I can. However, I'm trying to create a function that do many operations in a string. They work individually but when I try to set them as a chain into the function, they do not work:

def funtest(df, x):
    (
        df
        .replace({x: {'ä': 'ae', 'ö': 'oe', 'ü': 'ue', 'β': 'ss'}}, regex = True, inplace = True)
        .replace({x: '\s{2,}'}, ' ')
        .replace({x: '^\s '}, ' ')
        .replace({x: '\s $'}, ' ')
    )
    return df

 funteste(df, 'strings')

enter image description here

Can anyone show me how I solve this?

CodePudding user response:

Do not set inplace=True for pandas.DataFrame.replace if you want chain as it then does

performs operation inplace and returns None.

Just assign result back, consider simple example

import pandas as pd
df = pd.DataFrame({'x':['A','B','C','D','E']})
df = df.replace({'x':{'A':'a'}}).replace({'x':{'C':'c'}}).replace({'x':{'E':'e'}})
print(df)

output

   x
0  a
1  B
2  c
3  D
4  e

CodePudding user response:

This is your fixed code:

def funtest(df, x):
    return (
        df
        .replace({x: {'ä': 'ae', 'ö': 'oe', 'ü': 'ue', 'β': 'ss'}}, regex = True)
        .replace({x: '\s{2,}'}, ' ')
        .replace({x: '^\s '}, ' ')
        .replace({x: '\s $'}, ' ')
    )

 funteste(df, 'strings')

I've done 2 things.

  1. Removed the inplace=True that was making your code fail because the next operation was running on a Nonetype.
  2. changed the return place since we are no longer operating inplace, we need to return the result of all operations.

Is this good for you?

  • Related