我有一个包含多列的大型数据帧(示例如下所示(。我想通过将一个特定(人口列(列的值除以 1000 来更新该列的值。
City Population
Paris 23456
Lisbon 123466
Madrid 1254
Pekin 86648
我试过了 df['Population'].apply(lambda x: int(str(x))/1000)
和
df['Population'].apply(lambda x: int(x)/1000)
都给我错误
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
但是,从错误来看,似乎您的Series
某处'...'
了不可解析的字符串,并且需要进一步清理数据。