apply(lambda)
时间: 2023-06-21 09:10:30 浏览: 113
`apply(lambda)` is a method in pandas library which is used to apply a function to each element or row of a pandas dataframe or series. The `lambda` function is a small anonymous function which takes an argument and returns a value. It is used when we need a simple function for a short period of time.
For example, suppose we have a pandas series with numerical values and we want to square each value in the series. We can use `apply(lambda)` to achieve this as follows:
```
import pandas as pd
# create a pandas series
s = pd.Series([1, 2, 3, 4, 5])
# apply lambda function to square each value in the series
s_squared = s.apply(lambda x: x**2)
print(s_squared)
```
Output:
```
0 1
1 4
2 9
3 16
4 25
dtype: int64
```
In the above example, we have used `apply(lambda)` to apply a lambda function to each element of the series to square it.
阅读全文