尝试将对象转换为浮动类型以用于人工和材料成本。
数据类型:
Cntr No object
Amount in Estimate Currency float64
Labour Cost object
Material Cost object
type object
dtype: object
输出数据:
OddU 6167142 543.42 192 351.42
输入:以下代码没有运气
#convert to float
df_co.iloc[:,2:3].apply(lambda x : x.str.extract('(\d+)',expand=False).astype(float))
df_co['Labour Cost'].astype(float)
答案 0 :(得分:1)
不幸的是有错误,所以在分配回float
后,列再次转换为object
:
所以可能的解决方案是:
np.random.seed(2018)
np.random.seed(123)
df_co = pd.DataFrame(np.random.choice(['sa5', 's7s'], size=(5, 4)))
print(df_co)
0 1 2 3
0 sa5 s7s sa5 sa5
1 sa5 sa5 sa5 s7s
2 s7s sa5 s7s s7s
3 sa5 s7s sa5 s7s
4 sa5 s7s s7s sa5
c = df_co.columns[2:3]
print (c)
#remove columns which need convert
df = df_co.drop(c, axis=1)
#convert columns
df1 = df_co[c].apply(lambda x : x.str.extract('(\d+)',expand=False)).astype(float)
#join together and last reindex for same ordering
df_co = df.join(df1).reindex(columns=df_co.columns)
print(df_co)
0 1 2 3
0 sa5 s7s 5.0 sa5
1 sa5 sa5 5.0 s7s
2 s7s sa5 7.0 s7s
3 sa5 s7s 5.0 s7s
4 sa5 s7s 7.0 sa5
print(df_co.dtypes)
0 object
1 object
2 float64
3 object
dtype: object