Python - Pandas 输出限制列

标签 python pandas dataframe io

在处理 Pandas 时,我试图打印对象运动学和角度状态的分析。我这样做的代码如下:

def displayData(tList, xList, zList, dxList, dzList, thetaList, dthetaList, Q_sList):
    states = pd.DataFrame({ 't' : tList,
                            'x' : xList,
                            'z' : zList,
                            'dx' : dxList,
                            'dz' : dzList,
                            'theta' : thetaList,
                            'dtheta' : dthetaList,
                            'Q_s' : Q_sList})

    print states[['t', 'x', 'z', 'dx', 'dz', 'theta', 'dtheta', 'Q_s']]

但是,当被要求打印数据时,输出会在超过特定点时打散列:

          t           x           z          dx         dz     theta  \
0     0.000 -500.000000 -100.000000  100.000000  -0.000000  0.000000   
1     0.005 -499.500000 -100.000000   99.999983   0.057692 -0.000577   
2     0.010 -499.000000  -99.999712   99.999933   0.115329 -0.001153
...     ...         ...         ...         ...        ...       ... 

        dtheta       Q_s  
0    -0.115385 -0.038462  
1    -0.115274 -0.038425  
2    -0.115163 -0.038388
...        ...       ...

由于我当时有成千上万的状态要打印,我希望 pandas 不会像这样分解表格,这样我就可以分析一个给定的状态,而不必滚动来选择其余的两个数据字段。有什么方法可以定义要打印的特定尺寸,以免发生这种情况?

最佳答案

有两个有用的settings在这种情况下可以使用:pd.options.display.widthpd.options.display.expand_frame_repr

这是一个小演示:

In [118]: pd.options.display.expand_frame_repr
Out[118]: True

In [119]: pd.options.display.width = 50

In [120]: df
Out[120]:
       t      x           z          dx  \
0  0.000 -500.0 -100.000000  100.000000
1  0.005 -499.5 -100.000000   99.999983
2  0.010 -499.0  -99.999712   99.999933

         dz     theta    dtheta       Q_s
0 -0.000000  0.000000 -0.115385 -0.038462
1  0.057692 -0.000577 -0.115274 -0.038425
2  0.115329 -0.001153 -0.115163 -0.038388

In [121]: pd.options.display.width = 100

In [122]: df
Out[122]:
       t      x           z          dx        dz     theta    dtheta       Q_s
0  0.000 -500.0 -100.000000  100.000000 -0.000000  0.000000 -0.115385 -0.038462
1  0.005 -499.5 -100.000000   99.999983  0.057692 -0.000577 -0.115274 -0.038425
2  0.010 -499.0  -99.999712   99.999933  0.115329 -0.001153 -0.115163 -0.038388

In [131]: pd.options.display.width = 40

In [132]: df
Out[132]:
       t      x           z  \
0  0.000 -500.0 -100.000000
1  0.005 -499.5 -100.000000
2  0.010 -499.0  -99.999712

           dx        dz     theta  \
0  100.000000 -0.000000  0.000000
1   99.999983  0.057692 -0.000577
2   99.999933  0.115329 -0.001153

     dtheta       Q_s
0 -0.115385 -0.038462
1 -0.115274 -0.038425
2 -0.115163 -0.038388


In [125]: pd.options.display.expand_frame_repr = False

In [126]: df
Out[126]:
       t      x           z          dx        dz     theta    dtheta       Q_s
0  0.000 -500.0 -100.000000  100.000000 -0.000000  0.000000 -0.115385 -0.038462
1  0.005 -499.5 -100.000000   99.999983  0.057692 -0.000577 -0.115274 -0.038425
2  0.010 -499.0  -99.999712   99.999933  0.115329 -0.001153 -0.115163 -0.038388

In [127]: pd.options.display.width
Out[127]: 30

或者,您可以使用 set_options()方法

这里是所有 diplay 选项的列表:

In [128]: pd.options.display.
pd.options.display.chop_threshold     pd.options.display.latex              pd.options.display.mpl_style
pd.options.display.colheader_justify  pd.options.display.line_width         pd.options.display.multi_sparse
pd.options.display.column_space       pd.options.display.max_categories     pd.options.display.notebook_repr_html
pd.options.display.date_dayfirst      pd.options.display.max_columns        pd.options.display.pprint_nest_depth
pd.options.display.date_yearfirst     pd.options.display.max_colwidth       pd.options.display.precision
pd.options.display.encoding           pd.options.display.max_info_columns   pd.options.display.show_dimensions
pd.options.display.expand_frame_repr  pd.options.display.max_info_rows      pd.options.display.unicode
pd.options.display.float_format       pd.options.display.max_rows           pd.options.display.width
pd.options.display.height             pd.options.display.max_seq_items
pd.options.display.large_repr         pd.options.display.memory_usage

关于Python - Pandas 输出限制列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38904049/

相关文章:

r - 计算某个元素在 data.frame 的列中出现的次数

python - Python try/except取决于变量

python - 在 pandas 中使用 groupby 过滤数据

python - django rest framework 错误 Cannot apply DjangoModelPermissions on a view that does not set `.queryset` or have a `.get_queryset()` method

python - 嵌套 for 循环遍历 python 列表

python - PANDAS 从开始位置连续计算连续日期

python - 按列值连接数据帧

python - 使用 Pandas 动态替换其他列中的值

python - 对包裹的 2D 数组中的子数组进行高效 Numpy 采样

python - download_to_filename 创建一个空文件(谷歌云存储)