为什么某些命令会导致相对于前面命令的错误?
这是一个熊猫问题。
尝试将其复制到 Jupyter Notebook 中:
In [1]: df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],
index=['cobra', 'viper', 'sidewinder'],
columns=['max_speed', 'shield'])
df
In [2]: df.pop('shield') # Return as series.
In [3]: pd.DataFrame(df.pop('shield')) # Return as DataFrame.
然后将其反转为以下序列
在[1]中
在[3]中
在[2]
为什么第三个Out[-]总是出错?
我经常遇到这种错误。这是缓存问题吗?冗余?出现这样的错误的原因是什么?
This is a pandas question.
Try to copy this in Jupyter Notebook:
In [1]: df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],
index=['cobra', 'viper', 'sidewinder'],
columns=['max_speed', 'shield'])
df
In [2]: df.pop('shield') # Return as series.
In [3]: pd.DataFrame(df.pop('shield')) # Return as DataFrame.
Then inverse it to the sequence of
In[1]
In[3]
In[2]
Why the 3rd Out[-] always cause an error?
I oftentimes encounter this kinds of error. Is this a cache issue? Redundancy? What is the reason why such error occurs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您的代码会生成预期错误:
这里 < code>DataFrame.pop 从原始 Dataframe 中获取列
shield
,创建 Series 并从原始数据中删除:因此在
之后的
:df
中没有列pop太失败了获取列
shield
,因为df
中不存在:I think your code generate expected error:
Here
DataFrame.pop
take columnshield
from original Dataframe, create Series and drop from original:So no column in
df
afterpop
:So failed get column
shield
, because not exist indf
:pop
在返回数据帧时从数据帧中删除该系列。无论如何,你不能连续调用它两次。
pop
removes the series from the dataframe while returning it.You can't call it twice in sequence no matter what.
在第二行中,您正在删除盾牌
在第三留置权中你又流行了
删除第二个代码将按预期工作
in 2 nd line you are deleting shield
in third lien you are pop again
removing 2nd and code will work as expected