pandas 将列从str转换为float,但当我尝试减去两列时,得到ValueError

332nm8kg  于 2023-03-11  发布在  其他
关注(0)|答案(3)|浏览(179)

我将数据从csv文件导入到Pandas数据框中。我从字符串中删除了非数字值,即所有值前面的“$”。然后我将列转换为float数据类型。转换后我运行print(df.dtypes),它将所有列显示为float 64。在print语句后,我试图从另一列中减去一列,但得到一个错误消息:

line 23, in <module>
    Price_Diff = df["HTB_Price" - "McMaster_Price"]
TypeError: unsupported operand type(s) for -: 'str' and 'str'

下面是我的代码

import pandas as pd
import matplotlib.pyplot as mp
import numpy as np

# Reads the csv and create a dataframe titled "df"
df = pd.read_csv('Example Price Dataset.csv', sep='\s*,\s*', engine='python')

# Removes the "$" from all columns using a left strip
df['HTB_Price'] = df['HTB_Price'].map(lambda x: x.lstrip('$'))
df['McMaster_Price'] = df['McMaster_Price'].map(lambda x: x.lstrip('$'))
df['Motion_Price'] = df['Motion_Price'].map(lambda x: x.lstrip('$'))
df['MRO_Price'] = df['MRO_Price'].map(lambda x: x.lstrip('$'))

# Converts each column to a float datatype instead of a string
df["HTB_Price"] = df["HTB_Price"].astype(float)
df["McMaster_Price"] = df["McMaster_Price"].astype(float)
df["Motion_Price"] = df["Motion_Price"].astype(float)
df["MRO_Price"] = df["MRO_Price"].astype(float)
print(df.dtypes)

#
Price_Diff = df["HTB_Price" - "McMaster_Price"]

# Prints the dataframe
# print(df.dtypes)

错误在Price_Diff行上,我不知道为什么它会抛出一个关于不能相互减去字符串的错误,而就在这一行之前,我正在检查数据类型,它说它们都是浮点型。
我希望每列中的值相减并放入变量Price_Diff中

z2acfund

z2acfund1#

代码中的问题与计算Price_Diff的行有关。您尝试减去两个字符串“HTB_Price”-“McMaster_Price”,而不是 Dataframe df["HTB_Price"] - df["McMaster_Price"]的实际列。以下是更正后的代码:

# Calculates the price difference between two columns
Price_Diff = df["HTB_Price"] - df["McMaster_Price"]
ssm49v7z

ssm49v7z2#

您确实可以尝试Price_Diff = df["HTB_Price"] - df["McMaster_Price"],但也存在string-based interface

df.eval("HTB_Price - McMaster_Price")

存在用于过滤的类似接口:

df.query("HTB_Price < McMaster_Price")

您甚至可以直接添加列来就地修改原始 Dataframe :

>>> df.eval('C = A + B', inplace=True)
>>> df
   A   B   C
0  1  10  11
1  2   8  10
2  3   6   9
3  4   4   8
4  5   2   7
rvpgvaaj

rvpgvaaj3#

你想写的是:

Price_Diff = df["HTB_Price"] - df["McMaster_Price"]

括号内的部分与 Dataframe 的索引有关,所以在这里,Python只是告诉你它不能从"HTB_Price"中减去"McMaster_Price"

相关问题