【问题标题】:reshape a pandas dataframe index to columns将熊猫数据框索引重塑为列
【发布时间】:2017-04-21 18:29:19
【问题描述】:

考虑下面的熊猫系列对象,

index = list('abcdabcdabcd')
df = pd.Series(np.arange(len(index)), index = index)

我想要的输出是,

   a  b   c   d
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11

我在 pd.pivot_table、pd.unstack 上做了一些努力,可能解决方案在于正确使用其中之一。我最近到达的是

df.reset_index(level = 1).unstack(level = 1)

但这并没有给我我正在寻找的输出

// 这里更接近于期望的输出,但我无法处理索引分组。

df.to_frame().set_index(df1.values, append = True, drop  = False).unstack(level = 0)

     a    b     c     d
0   0.0  NaN   NaN   NaN
1   NaN  1.0   NaN   NaN
2   NaN  NaN   2.0   NaN
3   NaN  NaN   NaN   3.0
4   4.0  NaN   NaN   NaN
5   NaN  5.0   NaN   NaN
6   NaN  NaN   6.0   NaN
7   NaN  NaN   NaN   7.0
8   8.0  NaN   NaN   NaN
9   NaN  9.0   NaN   NaN
10  NaN  NaN  10.0   NaN
11  NaN  NaN   NaN  11.0

【问题讨论】:

    标签: python pandas dataframe pivot-table


    【解决方案1】:

    使用cumcount 获取新索引值并使用pivot 进行重塑的更通用解决方案:

    # Reset the existing index, and construct the new index values.
    df = df.reset_index()
    df.index = df.groupby('index').cumcount()
    
    # Pivot and remove the column axis name.
    df = df.pivot(columns='index', values=0).rename_axis(None, axis=1)
    

    结果输出:

       a  b   c   d
    0  0  1   2   3
    1  4  5   6   7
    2  8  9  10  11
    

    【讨论】:

    • 确实很周到
    【解决方案2】:

    如果索引始终以相同的顺序循环,并且您知道“句点”(在本例中为 4),则这是一种可行的方法:

    >>> pd.DataFrame(df.values.reshape(-1,4), columns=list('abcd'))
       a  b   c   d
    0  0  1   2   3
    1  4  5   6   7
    2  8  9  10  11
    >>>
    

    【讨论】:

    • 索引循环将按照相同的顺序。但是,索引长度不一定能被 4 整除,例如 ['a','b','a','b','a'] 不能在 numpy 中重新整形为 (2,2) 数组。但是,我猜在熊猫中,它将填充 NaN 值。希望我说得通:)
    • @SirajS。换句话说,它可能不会在循环结束时终止?
    • 是的。元素的长度可能不能与列的长度完全整除。在这种情况下 numpy.reshape() 将不起作用
    猜你喜欢
    • 2019-06-30
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多