我有一个包含多列的大型数据框(示例如下所示)。我想通过将一个特定(人口列)列的值除以 1000 来更新它的值。
City Population
Paris 23456
Lisbon 123466
Madrid 1254
Pekin 86648
Run Code Online (Sandbox Code Playgroud)
我试过了
df['Population'].apply(lambda x: int(str(x))/1000)
和
df['Population'].apply(lambda x: int(x)/1000)
Run Code Online (Sandbox Code Playgroud)
两者都给我错误
ValueError:int() 的无效文字,基数为 10:'...'
如果您DataFrame确实看起来如此,那么第二个示例应该可以正常工作(int甚至不需要):
In [16]: df
Out[16]:
City Population
0 Paris 23456
1 Lisbon 123466
2 Madrid 1254
3 Pekin 86648
In [17]: df['Population'].apply(lambda x: x/1000)
Out[17]:
0 23.456
1 123.466
2 1.254
3 86.648
Name: Population, dtype: float64
In [18]: df['Population']/1000
Out[18]:
0 23.456
1 123.466
2 1.254
3 86.648
Run Code Online (Sandbox Code Playgroud)
然而,从错误,好像你有不可分析串'...'在你的地方Series,并需要对数据进行进一步清理。
| 归档时间: |
|
| 查看次数: |
21487 次 |
| 最近记录: |