'Python create a function with a chain within
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')
Can anyone show me how I solve this?
Solution 1:[1]
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
Solution 2:[2]
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.
- Removed the
inplace=Truethat was making your code fail because the next operation was running on a Nonetype. - 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?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Daweo |
| Solution 2 | scr |

