簡體   English   中英

Python pandas 加載 Excel 文件將列值(變量)分配給另一列(字符串)

[英]Python pandas load Excel file assign column values (variable) to another column (string)

姓名 金額 Rajesh 50 Mahesh 20 Jon 60 Jack 85

此數據在我的 excel 文件中 名稱是恆定的且數量可變(每月更改)我想要 Rajesh + Jon = 110 的總量

我已經完成了以下程序。它顯示以下錯誤名稱“Rajesh”未定義我的代碼如下

import pandas as pd
top=pd.read_excel(r'E:\Python\ipc_python.xlsx',header=0,usecols="A:B",sheet_name="add")

Amount1=(top.head(4))
Series_topsheet=top['Amount'].to_list()

Legend=top['Name'].to_list()
Legend=Amount1
Total_amount_Rajesh_Jon = Rajesh + Jon
print(Total_amount_Rajesh_Jon)

您試圖引用不存在的變量。

"Rajesh""Jon"是未定義的變量,即使它們是 dataframe 中的列名。

嘗試這個:

Total_amount_Rajesh_Jon = top["Rajesh"] + top["Jon"]

您可能需要將 type 轉換為float

Total_amount_Rajesh_Jon = float(top["Rajesh"]) + float(top["Jon"])

您沒有定義名為 Rajesh 和 Jon 變量的名稱。 第一步:你走 rajesh 行。 第二步:走喬恩行。

rajesh = top.loc[top.Name == "Rajesh"]
jon= top.loc[top.Name == "Jon"]

然后,您將金額屬性相加

Total_amount_Rajesh_Jon = rajesh.Amount + jon.Amount
print(Total_amount_Rajesh_Jon)

祝你有美好的一天。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM