【问题标题】:Display pandas columns - wide to long显示 pandas 列 - 从宽到长
【发布时间】:2019-11-13 08:39:39
【问题描述】:

假设我在 Pandas Dataframe 中有一行列标题和相关值:

print df

A       B       C       D       E       F       G       H       I       J       K
1       2       3       4       5       6       7       8       9       10      11   

我该如何像下面这样显示它们:

print df

A       B       C       D       E       
1       2       3       4       5       

F       G       H       I       J       
6       7       8       9       10      

K
11

【问题讨论】:

标签: python pandas python-2.7


【解决方案1】:

自定义函数

def new_repr(self):
    g = self.groupby(np.arange(self.shape[1]) // 5, axis=1)
    return '\n\n'.join([d.to_string() for _, d in g])

print(new_repr(df))

   A  B  C  D  E
0  1  2  3  4  5

   F  G  H  I   J
0  6  7  8  9  10

    K
0  11

【讨论】:

    【解决方案2】:
    pd.set_option('display.width', 20)
    pd.set_option('display.expand_frame_repr', True)
     df
       A  B  C  D  E  \
    0  1  2  3  4  5   
    
       F  G  H  I   J  \
    0  6  7  8  9  10   
    
        K  
    0  11 
    

    【讨论】:

    • 赞成,但使用函数作为最终代码将被转换为 exe,我不想赌 pd.options 不可编译。
    猜你喜欢
    • 2016-10-05
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    相关资源
    最近更新 更多