pandas 熊猫:`item` 已被弃用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/57390363/
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
Pandas: `item` has been deprecated
提问by Joey Coder
So far I used this line of code here:
到目前为止,我在这里使用了这行代码:
max_total_gross = event_data["max_total_gross"].loc[event_data["event_id"] == event_id].item()
Since I updated Pandas I receive the future warning:
由于我更新了 Pandas,我收到了未来的警告:
/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:12: FutureWarning:
item
has been deprecated and will be removed in a future version if sys.path[0] == '':
/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:12: FutureWarning:
item
已被弃用,如果 sys.path[0] == '' ,将在未来版本中删除:
I tried to fix it that way, but it's not the same outcome:
我试图以这种方式修复它,但结果不一样:
event_data.loc[event_data.event_id == event_id, 'max_total_gross']
I expected a single integer.
我期望一个整数。
采纳答案by jezrael
If need first matched value use iter
with next
, advantage is if no value is matched is returned default value:
如果需要首先匹配的值使用iter
with next
,优点是如果没有匹配的值则返回默认值:
s = event_data.loc[event_data.event_id == event_id, 'max_total_gross']
out = next(iter(s), 'no match')
print (out)
回答by MoRe
回答by cxrodgers
The method item()
is still useful if you want to assert that the Series has length exactly 1, and also get that single value at the same time. I recommend replacing:
item()
如果您想断言 Series 的长度恰好为 1,并且同时获得该单个值,则该方法仍然很有用。我建议更换:
result = ser.item()
with:
和:
result = ser.values.item()
which should do what you want.
哪个应该做你想做的。