import pandas as pd # create a pandas Series s = pd.Series([1, 2, 3, 4, 5]) # define a function that squares a number def square(x): return x**2 # apply the function to the Series s_squared = s.apply(square) print(s_squared)
0 1 1 4 2 9 3 16 4 25 dtype: int64
# round every element in the Series to 1 decimal place s_rounded = s.apply(round, args=(1,)) print(s_rounded)
0 1 1 2 2 3 3 4 4 5 dtype: int64Finally, we should note that the apply() method is part of the pandas library, which we import with the line "import pandas as pd".