Use the aggregate
function instead, which allows more options for the func
parameter:
res = df.aggregate({'col1': minor, 'col2': minor, 'col3': minor, 'col4': do_nothing})
print(res)
Output (in the context of the script in question):
col1 col2 col3 col4
0 True False False True
1 False False False True
2 False False False False
3 False True False True
An option to write all this a bit “smarter” is to make the literal 2
a variable and to replace do_nothing
by a name that better reflects the way the input is handled:
import pandas as pd
d = {
'col1': [1, 2, 4, 7],
'col2': [3, 4, 9, 1],
'col3': [5, 2, 11, 4],
'col4': [True, True, False, True]
}
df = pd.DataFrame(data=d)
# identity function:
copy = lambda x: x
# lt (less than arg). returns a function that compares to the bound argument:
def lt(arg):
return lambda x: x < arg
res = df.aggregate({'col1': lt(2), 'col2': lt(2), 'col3': lt(2), 'col4': copy})
print(res)
Same output as above.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…