map( lambda s: s.replace(...), row )
or use a list comprehension
[s.replace(...) for s in row]
Answer from Alexander Gessler on Stack Overflowmap( lambda s: s.replace(...), row )
or use a list comprehension
[s.replace(...) for s in row]
The idiomatic Python here is probably to use a list comprehension:
row = [ x.replace('\'', "\\'") for x in row ]
python 3.x - Pandas to replace value using map function - Stack Overflow
python - Is there a difference between `Series.replace()` and `Series.map()` in pandas? - Stack Overflow
Python Map, Lambda, and string.replace - Stack Overflow
dictionary - Replace string using MAP and LAMBDA in Python - Stack Overflow
I don't know if i correctly understand your question, but you can map the dict values across the columns as follows..
Example DataFrame:
>>> df
A B C D
0 no no no yes
1 yes yes yes no
2 yes no yes no
3 no yes no yes
Result:
>>> for col in 'ABCD':
df[col] = df[col].map({'yes':True, 'no':False})
>>> df
A B C D
0 False False False True
1 True True True False
2 True False True False
3 False True False True
Map function can only be used to process single value(not a list). You can split the list into different columns and use map to replace values. Then you can integrate all the columns together to achieve replacement.
You can use str.translate for replacing multiple characters at once. str.maketrans helps you create the required mapping:
eSToAvoid = 'éêèÉÊÈ'
textFile.translate(str.maketrans(eSToAvoid, 'e' * len(eSToAvoid)))
While the str.replace can only replace one substring with another, re.sub can replace a pattern.
In [55]: eSToAvoid = 'éêèÉÊÈ'
In [58]: import re
test cases:
In [61]: re.sub(r'[éêèÉÊÈ]', 'e', 'foobar')
Out[61]: 'foobar'
In [62]: re.sub(r'[éêèÉÊÈ]', 'e', eSToAvoid)
Out[62]: 'eeeeee'
In [63]: re.sub(r'[éêèÉÊÈ]', 'e', 'testingè,É foobar è É')
Out[63]: 'testinge,e foobar e e'
The string replace approach is:
In [70]: astr = 'testingè,É foobar è É'
...: for e in eSToAvoid:
...: astr = astr.replace(e,'e')
...:
In [71]: astr
Out[71]: 'testinge,e foobar e e'
the replace is applied sequentially to astr. This can't be expressed as a list comprehension (or map). A list comprehensions most naturally replaces a loop that collects its results in a list (with list.append).
There's nothing wrong with the for loop. It's actually faster:
In [72]: %%timeit
...: astr = 'testingè,É foobar è É'
...: for e in eSToAvoid:
...: astr = astr.replace(e,'e')
...:
...:
1.37 µs ± 8.96 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [73]: timeit re.sub(r'[éêèÉÊÈ]', 'e', 'testingè,É foobar è É')
2.79 µs ± 15.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [77]: timeit astr.translate(str.maketrans(eSToAvoid, 'e' * len(eSToAvoid)))
2.56 µs ± 14.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
reduce
In [93]: from functools import reduce
In [96]: reduce(lambda s,e: s.replace(e,'e'),eSToAvoid, 'testingè,É foobar è É' )
Out[96]: 'testinge,e foobar e e'
In [97]: timeit reduce(lambda s,e: s.replace(e,'e'),eSToAvoid, 'testingè,É foobar è É' )
2.11 µs ± 32.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
For fun you could also explore some of the idea presented here:
Cleanest way to combine reduce and map in Python
You want to 'accumulate' changes, and to do that, you need some sort of accumulator, something that hangs on to the last replace. itertools has an accumulate function, and Py 3.8 introduced a := walrus operator.
generator
In [110]: def foo(astr, es):
...: for e in es:
...: astr = astr.replace(e,'e')
...: yield astr
...:
In [111]: list(foo(astr, eSToAvoid))
Out[111]:
['testingè,É foobar è É',
'testingè,É foobar è É',
'testinge,É foobar e É',
'testinge,e foobar e e',
'testinge,e foobar e e',
'testinge,e foobar e e']
Or [s for s in foo(astr, eSToAvoid)] in place of the list(). This highlights that fact that a list comprehension returns a list of strings, even if the strings accumulate the changes.