Reshape of pandas series?

前端 未结 5 1226
轮回少年
轮回少年 2020-12-17 10:32

It looks to me like a bug in 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 l

5条回答
  •  渐次进展
    2020-12-17 11:02

    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:

    1. convert DataFrame to numpy ndarray
    2. do reshape
    3. convert back

    e.g.

    a = pd.DataFrame([[1,2,3],[4,5,6]])
    b = a.as_matrix().reshape(3,2)
    a = pd.DataFrame(b)
    

提交回复
热议问题