重塑 pandas 系列? [英] Reshape of pandas series?

查看:377
本文介绍了重塑 pandas 系列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,这就像pandas.Series中的错误.

It looks to me like a bug in pandas.Series.

a = pd.Series([1,2,3,4])
b = a.reshape(2,2)
b

b的类型为Series,但是无法显示,最后一条语句给出了异常,很长,最后一行是"TypeError:%d格式:需要数字,而不是numpy.ndarray". b.shape返回(2,2),这与它的Series系列矛盾.我想也许pandas.Series没有实现重塑功能,我是从np.array调用版本吗?有人也看到这个错误吗?我在大熊猫0.9.1.

b has type Series but can not be displayed, the last statement gives exception, very lengthy, the last line is "TypeError: %d format: a number is required, not numpy.ndarray". b.shape returns (2,2), which contradicts its type Series. I am guessing perhaps pandas.Series does not implement reshape function and I am calling the version from np.array? Anyone see this error as well? I am at pandas 0.9.1.

推荐答案

您可以致电系列的 values 数组上的reshape :

You can call reshape on the values array of the Series:

In [4]: a.values.reshape(2,2)
Out[4]: 
array([[1, 2],
       [3, 4]], dtype=int64)

我实际上认为将reshape应用于系列并不总是很有意义(您是否忽略了索引?),并且您认为这只是numpy的重塑是正确的:

I actually think it won't always make sense to apply reshape to a Series (do you ignore the index?), and that you're correct in thinking it's just numpy's reshape:

a.reshape?
Docstring: See numpy.ndarray.reshape

a.reshape?
Docstring: See numpy.ndarray.reshape

那是我同意的事实,那就是让您尝试执行此操作看起来像个错误.

这篇关于重塑 pandas 系列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆