Thursday, March 14, 2024

Apply any function of your choice

Pandas.apply allow the users to pass a function and apply it on every single value of the Pandas series.


import pandas as pd
data = {"K1": [12, 23], "K2": [25,50]}
df = pd.DataFrame(data, index=['R1', 'R2'])
print(df.apply(lambda x: x-2))

Output:
   K1  K2
R1  10  23
R2  21  48

No comments:

Post a Comment