Pandas 是 Python 中一個強大的數據處理庫,廣泛應用于數據清洗、分析和處理。在 Pandas 中,調用函數是非常常見的操作,尤其是在對數據進行轉換、聚合或計算時。本文將詳細介紹如何在 Pandas 中調用函數,涵蓋常見的函數調用方式、應用場景以及一些高級用法。
在 Pandas 中,函數調用通常用于對 DataFrame 或 Series 中的數據進行操作。Pandas 提供了多種方式來調用函數,主要包括以下幾種:
sum()、mean()、max() 等,可以直接在 DataFrame 或 Series 上調用。apply() 方法:apply() 方法允許你對 DataFrame 或 Series 中的每個元素或每行/列應用自定義函數。map() 方法:map() 方法主要用于 Series,允許你對 Series 中的每個元素應用自定義函數。applymap() 方法:applymap() 方法用于對 DataFrame 中的每個元素應用自定義函數。agg() 方法:agg() 方法用于對 DataFrame 或 Series 進行聚合操作,可以同時應用多個函數。接下來,我們將詳細介紹這些方法的使用。
Pandas 提供了許多內置函數,可以直接在 DataFrame 或 Series 上調用。這些函數通常用于對數據進行簡單的統計或計算。
import pandas as pd
# 創建一個 Series
s = pd.Series([1, 2, 3, 4, 5])
# 調用內置函數
print(s.sum()) # 求和
print(s.mean()) # 求平均值
print(s.max()) # 求最大值
print(s.min()) # 求最小值
# 創建一個 DataFrame
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': [10, 20, 30, 40, 50]
})
# 調用內置函數
print(df.sum()) # 對每列求和
print(df.mean()) # 對每列求平均值
print(df.max()) # 對每列求最大值
print(df.min()) # 對每列求最小值
apply() 方法apply() 方法是 Pandas 中最常用的函數調用方式之一。它允許你對 DataFrame 或 Series 中的每個元素、每行或每列應用自定義函數。
apply()# 創建一個 Series
s = pd.Series([1, 2, 3, 4, 5])
# 定義一個自定義函數
def square(x):
return x ** 2
# 使用 apply() 方法
s_squared = s.apply(square)
print(s_squared)
apply()# 創建一個 DataFrame
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': [10, 20, 30, 40, 50]
})
# 定義一個自定義函數
def sum_row(row):
return row['A'] + row['B']
# 使用 apply() 方法對每行應用函數
df['Sum'] = df.apply(sum_row, axis=1)
print(df)
# 定義一個自定義函數
def double_column(col):
return col * 2
# 使用 apply() 方法對每列應用函數
df_doubled = df.apply(double_column)
print(df_doubled)
map() 方法map() 方法主要用于 Series,允許你對 Series 中的每個元素應用自定義函數或映射關系。
map() 方法# 創建一個 Series
s = pd.Series([1, 2, 3, 4, 5])
# 定義一個自定義函數
def add_one(x):
return x + 1
# 使用 map() 方法
s_plus_one = s.map(add_one)
print(s_plus_one)
map() 方法進行映射# 創建一個 Series
s = pd.Series(['a', 'b', 'c', 'd'])
# 定義一個映射關系
mapping = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
# 使用 map() 方法進行映射
s_mapped = s.map(mapping)
print(s_mapped)
applymap() 方法applymap() 方法用于對 DataFrame 中的每個元素應用自定義函數。
applymap() 方法# 創建一個 DataFrame
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': [10, 20, 30, 40, 50]
})
# 定義一個自定義函數
def square(x):
return x ** 2
# 使用 applymap() 方法
df_squared = df.applymap(square)
print(df_squared)
agg() 方法agg() 方法用于對 DataFrame 或 Series 進行聚合操作,可以同時應用多個函數。
agg()# 創建一個 Series
s = pd.Series([1, 2, 3, 4, 5])
# 使用 agg() 方法
result = s.agg(['sum', 'mean', 'max', 'min'])
print(result)
agg()# 創建一個 DataFrame
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': [10, 20, 30, 40, 50]
})
# 使用 agg() 方法
result = df.agg(['sum', 'mean', 'max', 'min'])
print(result)
# 使用 agg() 方法對不同列應用不同的聚合函數
result = df.agg({
'A': ['sum', 'mean'],
'B': ['max', 'min']
})
print(result)
Lambda 函數是一種匿名函數,通常用于簡單的操作。在 Pandas 中,Lambda 函數可以與 apply()、map() 等方法結合使用。
# 創建一個 Series
s = pd.Series([1, 2, 3, 4, 5])
# 使用 Lambda 函數
s_squared = s.apply(lambda x: x ** 2)
print(s_squared)
pipe() 方法pipe() 方法允許你將 DataFrame 或 Series 傳遞給一個函數,并將結果返回。這種方法通常用于鏈式操作。
# 定義一個自定義函數
def add_one(df):
return df + 1
# 使用 pipe() 方法
df_plus_one = df.pipe(add_one)
print(df_plus_one)
transform() 方法transform() 方法用于對 DataFrame 或 Series 中的每個元素應用函數,并返回與原始數據相同形狀的結果。
# 創建一個 DataFrame
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': [10, 20, 30, 40, 50]
})
# 使用 transform() 方法
df_transformed = df.transform(lambda x: x * 2)
print(df_transformed)
在 Pandas 中,調用函數是非常常見的操作,尤其是在對數據進行轉換、聚合或計算時。本文介紹了 Pandas 中常用的函數調用方式,包括直接調用內置函數、使用 apply()、map()、applymap()、agg() 等方法,以及一些高級用法如 Lambda 函數、pipe() 方法和 transform() 方法。掌握這些方法可以幫助你更高效地處理和分析數據。
希望本文對你理解 Pandas 中的函數調用有所幫助!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。