簡體   English   中英

將 pandas df 列中的值除以數字

[英]divide the values from pandas df column by number

我需要將列的每個值除以 25。下面是我的代碼:

df['Amount'] = df['Amount'].div(25)

我收到一個錯誤: TypeError: unsupported operand type(s) for /: 'str' and 'int'然后我嘗試使用以下代碼將 Object 數據類型轉換為 int:

df["Amount"] = df["Amount"].astype(str).astype(int)

錯誤信息: ValueError: invalid literal for int() with base 10: '0.0'

你可以試試這個:

df["Amount"] = df["Amount"]/25
print(df)

你可以嘗試pd.to_numeric function,如果你想指定它是一個浮點列,你可以嘗試 downcast 參數,盡管如果你不添加它很可能沒問題。

df["Amount"] = pd.to_numeric(df["Amount"], downcast='float')

暫無
暫無

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

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