import pandas as pd
import numpy as np
# 創建一個包含三個欄位的 DataFrame
df = pd.DataFrame({‘A’: [1, 2, 3], ‘B’: [4, 5, 6], ‘C’: [7, 8, 9]})
# 自定義函數計算 A 和 B 欄位的總和,然後將結果新增到 DataFrame 中
def sum_ab(row):
return row[‘A’] + row[‘B’]
df[‘sum_AB’] = df.apply(sum_ab, axis=1)
print(df)
![Python: 如何對 pandas.DataFrame 兩欄位運算後,增加到最後一欄? df['sum_AB'] = df.apply(sum_ab, axis=1) ; lambda函式 - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2023/03/20230314200417_4.png)
.apply()中
可使用lambda函式
df[‘sum_AB’] = df.apply(lambda row: row[‘A’] + row[‘B’], axis=1)
![Python: 如何對 pandas.DataFrame 兩欄位運算後,增加到最後一欄? df['sum_AB'] = df.apply(sum_ab, axis=1) ; lambda函式 - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2023/03/20230315080650_41.png)
推薦hahow線上學習python: https://igrape.net/30afN
df_Frame_data1:
![Python: 如何對 pandas.DataFrame 兩欄位運算後,增加到最後一欄? df['sum_AB'] = df.apply(sum_ab, axis=1) ; lambda函式 - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2023/03/20230316163343_96.png)
df_Frame_data1[“r”] = df_Frame_data1.apply(lambda df:
(df[“X”]**0.5 + df[“Y”]**0.5)**0.5, axis=1) #X**2+Y**2的筆誤
預設值為axis=0,
若沒寫axis=1 , 會出現 KeyError: ‘X’
![Python: 如何對 pandas.DataFrame 兩欄位運算後,增加到最後一欄? df['sum_AB'] = df.apply(sum_ab, axis=1) ; lambda函式 - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2023/03/20230316163604_75.png)
如果不熟悉apply()
就要取出X, Y
經過運算後生成新的r
再將 [原DF , r]
concat( axis=1 ) 起來
推薦hahow線上學習python: https://igrape.net/30afN
![Python: 如何對 pandas.DataFrame 兩欄位運算後,增加到最後一欄? df['sum_AB'] = df.apply(sum_ab, axis=1) ; lambda函式 - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2023/03/20240227224538_0.png)
![Python: 如何對 pandas.DataFrame 兩欄位運算後,增加到最後一欄? df['sum_AB'] = df.apply(sum_ab, axis=1) ; lambda函式 - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2023/03/20240227224559_0.png)
![Python: 如何對 pandas.DataFrame 兩欄位運算後,增加到最後一欄? df['sum_AB'] = df.apply(sum_ab, axis=1) ; lambda函式 - 儲蓄保險王](https://savingking.com.tw/wp-content/uploads/2023/03/20240227224722_0.png)
![Python: 自定義函數計算計程車車資(先typing,再用預設值), 巢狀字典以及typing.Union[ ], assert 斷言 Python: 自定義函數計算計程車車資(先typing,再用預設值), 巢狀字典以及typing.Union[ ], assert 斷言](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2022/09/20220923222039_57.png?quality=90&zoom=2&ssl=1&resize=350%2C233)

![Excel TQC考題202: 快樂小學學生名冊,自訂格式:0″公斤”;[紅色]”減”0″公斤”;[藍色]”完美身材” Excel TQC考題202: 快樂小學學生名冊,自訂格式:0″公斤”;[紅色]”減”0″公斤”;[藍色]”完美身材”](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2022/04/20220410142405_70.png?quality=90&zoom=2&ssl=1&resize=350%2C233)

![Python: matplotlib如何控制legend的位置? ax.legend(handles=[patch], loc=’upper left’, bbox_to_anchor=(6/10, 3/5) Python: matplotlib如何控制legend的位置? ax.legend(handles=[patch], loc=’upper left’, bbox_to_anchor=(6/10, 3/5)](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2023/05/20230502163945_79.png?quality=90&zoom=2&ssl=1&resize=350%2C233)
![Python 讀取 DOCX 圖片關聯:qn+find/findall 與 XPath 的實戰對照 from lxml import etree ; from docx.oxml.ns import qn; lxml.etree._Element.findall( f”.//{ qn(‘a:blip’) }” ) ; .get( qn(“r:embed”) ) #獲取 屬性名 ‘r:embed’ 的 屬性值(如: ‘rId4’) ; lxml.etree._Element.xpath( “//a:blip/@r:embed”, namespaces = NS) #/@r:embed = 獲取 屬性名 ‘r:embed’ 的 屬性值(如: ‘rId4’),使用.findall() 要先.findall()獲取List[_Element],再迴圈_Element.get()獲取屬性值, .xpath() 第一個參數path 使用”//a:blip/@r:embed” ,可直接獲取屬性值(List[str]如: [‘rId4’, ‘rId5’]) ; 如何對docx真實移除圖片瘦身? Python 讀取 DOCX 圖片關聯:qn+find/findall 與 XPath 的實戰對照 from lxml import etree ; from docx.oxml.ns import qn; lxml.etree._Element.findall( f”.//{ qn(‘a:blip’) }” ) ; .get( qn(“r:embed”) ) #獲取 屬性名 ‘r:embed’ 的 屬性值(如: ‘rId4’) ; lxml.etree._Element.xpath( “//a:blip/@r:embed”, namespaces = NS) #/@r:embed = 獲取 屬性名 ‘r:embed’ 的 屬性值(如: ‘rId4’),使用.findall() 要先.findall()獲取List[_Element],再迴圈_Element.get()獲取屬性值, .xpath() 第一個參數path 使用”//a:blip/@r:embed” ,可直接獲取屬性值(List[str]如: [‘rId4’, ‘rId5’]) ; 如何對docx真實移除圖片瘦身?](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2025/11/20251119130848_0_3fbf6b.png?quality=90&zoom=2&ssl=1&resize=350%2C233)




近期留言