Pandas Series 中的 agg() 方法有什麼作用?
Pandas Series 中的 agg() 方法用於在一個 Series 物件上應用一個或多個函式。使用 agg() 方法,我們可以同時對 Series 應用多個函式。
要同時使用多個函式,我們需要將這些函式名作為元素列表傳送給 agg() 函式。
示例
# import pandas package
import pandas as pd
# create a pandas series
s = pd.Series([1,2,3,4,5,6,7,8,9,10])
print(s)
# Applying agg function
result = s.agg([max, min, len])
print('Output of agg method',result)解釋
物件“s”包含 10 個整數元素,透過使用 agg() 方法,我們對這個 Series 物件“s”應用了一些聚合操作。聚合操作包括 min、max 和 len。
輸出
0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 dtype: int64 Output of agg method max 10 min 1 len 10 dtype: int64
在下面的示例中,Pandas Series 的 agg() 方法將返回一個包含每個函式結果的 Series。因此,輸出將類似於函式名稱後跟結果輸出值。
示例
# import pandas package
import pandas as pd
# create a pandas series
s = pd.Series([1,2,3,4,5,6,7,8,9,10])
print(s)
# Applying agg function
result = s.agg(mul)
print('Output of agg method',result)解釋
讓我們來看另一個示例,並使用 agg() 方法對 Series 物件應用單個函式。這裡我們將 mul 函式名作為引數傳遞給 agg() 函式。
輸出
0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 dtype: int64 Output of agg method 0 2 1 4 2 6 3 8 4 10 5 12 6 14 7 16 8 18 9 20 dtype: int64
arr() 方法的輸出與實際的 Series 物件“s”一起顯示在上面的程式碼塊中。此 mul 函式應用於 Series 元素,結果輸出作為另一個 Series 物件從 agg() 方法返回。
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP