Pandas:更新列的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42100186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:55:18  来源:igfitidea点击:

Pandas: Update values of a column

python-3.xpandas

提问by PyAnton

I have a large dataframe with multiple columns (sample shown below). I want to update the values of one particular (population column) column by dividing the values of it by 1000.

我有一个包含多列的大型数据框(示例如下所示)。我想通过将一个特定(人口列)列的值除以 1000 来更新它的值。

City     Population
Paris    23456
Lisbon   123466
Madrid   1254
Pekin    86648

I have tried df['Population'].apply(lambda x: int(str(x))/1000)

我试过了 df['Population'].apply(lambda x: int(str(x))/1000)

and

df['Population'].apply(lambda x: int(x)/1000)

Both give me the error

两者都给我错误

ValueError: invalid literal for int() with base 10: '...'

ValueError:int() 的无效文字,基数为 10:'...'

采纳答案by fuglede

If your DataFramereally does look as presented, then the second example should work just fine (with the intnot even being necessary):

如果您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

However, from the error, it seems like you have the unparsable string '...'somewhere in your Series, and that the data needs to be cleaned further.

然而,从错误,好像你有不可分析串'...'在你的地方Series,并需要对数据进行进一步清理。