更改一系列数据框中的元素值 python
Changing the value of elements in a series of a dataframe python
我想将以字符串形式读取的美元金额更改为浮点数。以下是数据框系列的摘录。我想去掉美元符号并将值转换为浮点数。执行此操作的最佳方法是什么?
0 [=11=]
1 6,800
2 ,600
3 9,200
4 8,100
Name: DemMedHomeValue, dtype: object
0 [=11=]
1 [=11=]
2 ,750
3 ,942
4 ,509
Name: DemMedIncome, dtype: object
使用 str.strip
for remove $
, str.replace
and astype
转换为 float
:
print df
DemMedHomeValue
0 [=10=]
1 6,800
2 ,600
3 9,200
4 8,100
df['DemMedHomeValue'] = df['DemMedHomeValue'].str.strip('$')
.str.replace(',', '.')
.astype(float)
print df
DemMedHomeValue
0 0.0
1 186.8
2 87.6
3 139.2
4 168.1
我想将以字符串形式读取的美元金额更改为浮点数。以下是数据框系列的摘录。我想去掉美元符号并将值转换为浮点数。执行此操作的最佳方法是什么?
0 [=11=]
1 6,800
2 ,600
3 9,200
4 8,100
Name: DemMedHomeValue, dtype: object
0 [=11=]
1 [=11=]
2 ,750
3 ,942
4 ,509
Name: DemMedIncome, dtype: object
使用 str.strip
for remove $
, str.replace
and astype
转换为 float
:
print df
DemMedHomeValue
0 [=10=]
1 6,800
2 ,600
3 9,200
4 8,100
df['DemMedHomeValue'] = df['DemMedHomeValue'].str.strip('$')
.str.replace(',', '.')
.astype(float)
print df
DemMedHomeValue
0 0.0
1 186.8
2 87.6
3 139.2
4 168.1