![]() |
VOOZH | about |
Series.apply() method in Pandas is used to apply a function to each element of a Series. It allows to transform, modify or categorize data easily by running a custom function or lambda function on every value.
Example: This example applies a function to double each value in a Series.
0 2 1 4 2 6 3 8 dtype: int64
Explanation: s.apply(lambda x: x * 2) applies the function to each element of s and each value is multiplied by 2 and returned as a new Series
Series.apply(func, convert_dtype=True, args=())
Parameters:
Return: Returns a new Pandas Series with modified values
Example 1: This example uses apply() with a custom function to classify each mark in the Series as Pass or Fail based on a condition.
0 Fail 1 Pass 2 Pass 3 Fail dtype: object
Explanation:
Example 2: This example uses apply() with a lambda function to increase each salary value in the Series by 10%.
0 11000.0 1 16500.0 2 22000.0 dtype: float64
Explanation:
Example 3: This example uses apply() to convert each numeric value into a formatted text string.
0 Value is 5 1 Value is 10 2 Value is 15 dtype: object
Explanation: