Python 更新熊猫的 iterrow 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25478528/
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
Updating value in iterrow for pandas
提问by lokheart
I am doing some geocoding work that I used seleniumto screen scrape the x-y coordinate I need for address of a location, I imported an xls file to panda dataframe and want to use explicit loop to update the rows which do not have the x-y coordinate, like below:
我正在做一些地理编码工作,我用来selenium屏幕刮取位置地址所需的 xy 坐标,我将一个 xls 文件导入到熊猫数据帧,并希望使用显式循环来更新没有 xy 坐标的行,例如以下:
for index, row in rche_df.iterrows():
if isinstance(row.wgs1984_latitude, float):
row = row.copy()
target = row.address_chi
dict_temp = geocoding(target)
row.wgs1984_latitude = dict_temp['lat']
row.wgs1984_longitude = dict_temp['long']
I have read Why doesn't this function "take" after I iterrows over a pandas DataFrame?and am fully aware that iterrow only gives us a view rather than a copy for editing, but what if I really to update the value row by row? Is lambdafeasible?
我已经阅读了为什么在我遍历 Pandas DataFrame 之后这个函数不“执行”?并且我完全知道 iterrow 只给我们一个视图而不是一个用于编辑的副本,但是如果我真的要逐行更新值怎么办?是否lambda可行?
采纳答案by Marius
The rows you get back from iterrowsare copies that are no longer connected to the original data frame, so edits don't change your dataframe. Thankfully, because each item you get back from iterrowscontains the current index, you can use that to access and edit the relevant row of the dataframe:
您返回的行iterrows是不再连接到原始数据框的副本,因此编辑不会更改您的数据框。值得庆幸的是,因为您返回的每个项目都iterrows包含当前索引,您可以使用它来访问和编辑数据框的相关行:
for index, row in rche_df.iterrows():
if isinstance(row.wgs1984_latitude, float):
row = row.copy()
target = row.address_chi
dict_temp = geocoding(target)
rche_df.loc[index, 'wgs1984_latitude'] = dict_temp['lat']
rche_df.loc[index, 'wgs1984_longitude'] = dict_temp['long']
In my experience, this approach seems slower than using an approach like applyor map, but as always, it's up to you to decide how to make the performance/ease of coding tradeoff.
根据我的经验,这种方法似乎比使用apply或 之类的方法慢map,但与往常一样,由您决定如何权衡编码的性能/易用性。

