【发布时间】:2019-10-27 04:38:40
【问题描述】:
我正在尝试转换我的数据框中的一列,该列的名称是“重量”,它具有 str 格式的值
例如:"175lbs"
我正在使用映射器将所有这些值转换为float
我使用映射器尝试了 lambda 函数
def WeightConverter(w):
return float(w[:len(w)-3])
df1['Weight'] = df1.Weight.map(lambda x : WeightConverter(x))
df1['Weight'].head()
但是,type(df1['Weight'][0]) 返回str。预期结果:175.0 类型为 float
【问题讨论】:
-
df1 是我的数据框
-
在
WeightConverter中你可以只使用return float(w[:-3]),否则这对我有用。 -
用apply代替map试试
-
任何使用 apply ansev 的例子
-
'float' 对象在执行 return float(w[:-3]) 时不可下标
标签: python-3.x pandas