为什么某些命令会导致相对于前面命令的错误?

发布于 2025-01-10 02:16:27 字数 550 浏览 3 评论 0原文

这是一个熊猫问题。

尝试将其复制到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你对谁都笑 2025-01-17 02:16:27

我认为您的代码会生成预期错误:

df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],
        index=['cobra', 'viper', 'sidewinder'],
        columns=['max_speed', 'shield'])

这里 < code>DataFrame.pop 从原始 Dataframe 中获取列 shield,创建 Series 并从原始数据中删除:

a = df.pop('shield') # Return as series.
print (a)
cobra         2
viper         5
sidewinder    8
Name: shield, dtype: int64

因此在 之后的 df 中没有列pop

print (df)
            max_speed
cobra               1
viper               4
sidewinder          7

太失败了获取列 shield,因为 df 中不存在:

b = pd.DataFrame(df.pop('shield')) # Return as DataFrame.
print (b)
KeyError: 'shield'

I think your code generate expected error:

df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],
        index=['cobra', 'viper', 'sidewinder'],
        columns=['max_speed', 'shield'])

Here DataFrame.pop take column shield from original Dataframe, create Series and drop from original:

a = df.pop('shield') # Return as series.
print (a)
cobra         2
viper         5
sidewinder    8
Name: shield, dtype: int64

So no column in df after pop:

print (df)
            max_speed
cobra               1
viper               4
sidewinder          7

So failed get column shield, because not exist in df:

b = pd.DataFrame(df.pop('shield')) # Return as DataFrame.
print (b)
KeyError: 'shield'
酒解孤独 2025-01-17 02:16:27

pop 在返回数据帧时从数据帧中删除该系列。

无论如何,你不能连续调用它两次。

pop removes the series from the dataframe while returning it.

You can't call it twice in sequence no matter what.

兰花执着 2025-01-17 02:16:27

在第二行中,您正在删除盾牌
在第三留置权中你又流行了
删除第二个代码将按预期工作

df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],
        index=['cobra', 'viper', 'sidewinder'],
        columns=['max_speed', 'shield'])


pd.DataFrame(df.pop('shield'))

in 2 nd line you are deleting shield
in third lien you are pop again
removing 2nd and code will work as expected

df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],
        index=['cobra', 'viper', 'sidewinder'],
        columns=['max_speed', 'shield'])


pd.DataFrame(df.pop('shield'))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文