Python 熊猫系列的重塑?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14390224/
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
Reshape of pandas series?
提问by szli
It looks to me like a bug in pandas.Series.
在我看来,它就像 pandas.Series 中的一个错误。
a = pd.Series([1,2,3,4])
b = a.reshape(2,2)
b
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.
b 有类型 Series 但不能显示,最后一条语句给出异常,很长,最后一行是“TypeError: %d format: a number is required, not numpy.ndarray”。b.shape 返回 (2,2),这与其类型系列相矛盾。我猜pandas.Series 可能没有实现重塑功能,我正在从np.array 调用版本?有人也看到这个错误吗?我在熊猫 0.9.1。
采纳答案by Andy Hayden
You can call reshapeon the valuesarray of the Series:
您可以调用系列reshape的值数组:
In [4]: a.values.reshape(2,2)
Out[4]:
array([[1, 2],
[3, 4]], dtype=int64)
I actually think it won't always make sense to apply reshapeto a Series (do you ignore the index?), and that you're correct in thinking it's just numpy's reshape:
我实际上认为应用于reshape系列并不总是有意义(您是否忽略了索引?),并且您认为它只是 numpy 的重塑是正确的:
a.reshape?Docstring: See numpy.ndarray.reshape
a.reshape?Docstring: See numpy.ndarray.reshape
that said, I agree the fact that it let's you try to do this looks like a bug.
也就是说,我同意让你尝试这样做的事实,这看起来像是一个错误。
回答by Chang She
The reshape function takes the new shape as a tuple rather than as multiple arguments:
reshape 函数将新形状作为元组而不是多个参数:
In [4]: a.reshape?
Type: function
String Form:<function reshape at 0x1023d2578>
File: /Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/numpy/core/fromnumeric.py
Definition: numpy.reshape(a, newshape, order='C')
Docstring:
Gives a new shape to an array without changing its data.
Parameters
----------
a : array_like
Array to be reshaped.
newshape : int or tuple of ints
The new shape should be compatible with the original shape. If
an integer, then the result will be a 1-D array of that length.
One shape dimension can be -1. In this case, the value is inferred
from the length of the array and remaining dimensions.
Reshape is actually implemented in Series and will return an ndarray:
Reshape 实际上是在 Series 中实现的,并且会返回一个 ndarray:
In [11]: a
Out[11]:
0 1
1 2
2 3
3 4
In [12]: a.reshape((2, 2))
Out[12]:
array([[1, 2],
[3, 4]])
回答by 176coding
you can directly use a.reshape((2,2))to reshape a Series, but you can not reshape a pandas DataFrame directly, because there is no reshape function for pandas DataFrame, but you can do reshape on numpy ndarray:
你可以直接使用a.reshape((2,2))reshape一个Series,但是你不能直接reshape一个pandas DataFrame,因为pandas DataFrame没有reshape函数,但是你可以在numpy ndarray上做reshape:
- convert DataFrame to numpy ndarray
- do reshape
- convert back
- 将 DataFrame 转换为 numpy ndarray
- 重塑
- 转换回来
e.g.
例如
a = pd.DataFrame([[1,2,3],[4,5,6]])
b = a.as_matrix().reshape(3,2)
a = pd.DataFrame(b)
回答by Akash Nayak
Just use this below code:
只需使用以下代码:
b=a.values.reshape(2,2)
I think it will help you. u can directly use only reshape() function.but it will give future warning
我想它会帮助你。你只能直接使用 reshape() 函数。但它会给出未来的警告

