Panda,如何在使用打印时缩短/隐藏数据帧中的列表



我使用了一个包含大量相关数据的数据帧,因此在执行此操作时可以轻松访问所有相关信息。看起来像

df
ion    m/n  qte   C1    C2   rCC  compte
0     H   1.00    1  0.1  0.25  0.50       2
1    H2   1.00    3  0.5  0.30  1.00       4
2  10B2   5.00    1  0.6  0.30  0.50       4
3  11B2   5.50    0  0.2  0.20  1.00       0
4   10B  10.00    0  0.2  0.20  1.00       0
5   11B  11.00    0  0.2  0.20  1.00       0
6   Si2  14.01    1  0.8  0.80  1.00       0
7   Fe2  26.90    1  0.8  0.35  0.65       3
8  Fe2*  27.90    1  0.5  0.50  1.00       7

在代码的后面,我添加了两列,每行都包含一个非常的长列表。这使得任何print(df(都不可读,因为添加新行后它看起来是这样的。

df with fluxC1 and fluxC2 columns 
ion  ...                                             fluxC2
0     H  ...  [0.0004506072467966082, 0.0004511067891697997,...
1    H2  ...  [9.65067757502627e-05, 9.663517466177602e-05, ...
2  10B2  ...  [9.65067757502627e-05, 9.663517466177602e-05, ...
3  11B2  ...  [0.0021039651287393384, 0.002105830883498985, ...
4   10B  ...  [0.0021039651287393384, 0.002105830883498985, ...
5   11B  ...  [0.0021039651287393384, 0.002105830883498985, ...
6   Si2  ...  [1.9595400763556396e-11, 1.966500053364854e-11...
7   Fe2  ...  [2.0668903644852728e-05, 2.070098966831758e-05...
8  Fe2*  ...  [2.030468908656194e-07, 2.0349733523508614e-07...

有没有什么方法可以让我打印我的df,使列表显示为[…]或类似的东西,使我打印的df看起来像这个

df
ion    m/n  qte   C1    C2   rCC  compte  fluxC1  fluxC2
0     H   1.00    1  0.1  0.25  0.50       2   [...]   [...]
1    H2   1.00    3  0.5  0.30  1.00       4   [...]   [...]
2  10B2   5.00    1  0.6  0.30  0.50       4   [...]   [...]
3  11B2   5.50    0  0.2  0.20  1.00       0   [...]   [...]
4   10B  10.00    0  0.2  0.20  1.00       0   [...]   [...]
5   11B  11.00    0  0.2  0.20  1.00       0   [...]   [...]
6   Si2  14.01    1  0.8  0.80  1.00       0   [...]   [...]
7   Fe2  26.90    1  0.8  0.35  0.65       3   [...]   [...]
8  Fe2*  27.90    1  0.5  0.50  1.00       7   [...]   [...]

我甚至还没有找到一种方法来打印一个列表为[…],或者其中只有很少的元素,比如打印一个四舍五入的列表(在df中没有四舍五舍五入(,所以我有点怀疑。

您可以使用Pandas提供的样式格式。对此有两种方法。一种是在每次要显示df的调用中,对数据帧(df(使用style.format((。

df.style.format({'fluxC1': '[...]', 'fluxC2': '[...]'})

您可以为列使用所需的任何样式。要记住的语法是使用dict,其中key是列名,value是您想要的样式,如上所述。或者,如果您有一些复杂的逻辑,请使用可调用函数。有关此方面的更多信息,请参阅文档。

另一种方法是为样式格式化设置pd选项,这样您就不必每次都传递格式了。这里有一种方法(为了更好地理解,使用示例数据帧(:

import string
import pandas as pd
# A Style formatter that styles only Lists which are of len > 2
def style_formatter(i):
if isinstance(i, list):
if len(i) > 2:
return '[...]'
return i
return i
pd.set_option('styler.format.formatter', style_formatter)
# Sample dataframe
df = pd.DataFrame({'colA': [[*string.ascii_letters] for i in range(0, 6)], 'colB': [i for i in range(0, 6)]})

默认df输出:

colA                                                colB
0   [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, ...   0
1   [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, ...   1
2   [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, ...   2
3   [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, ...   3
4   [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, ...   4
5   [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, ...   5

要设置输出样式,请将df调用为df.style。基于pd样式选项的自动样式后的输出示例:

colA    colB
0   [...]   0
1   [...]   1
2   [...]   2
3   [...]   3
4   [...]   4
5   [...]   5

严重注意:如果在大数据帧上运行样式设置,可能需要一段时间。更好的方法是获取数据,然后对其使用样式。例如df.head(20).style

这可能会对您有所帮助,我在处理更大的数据帧时经常使用它

pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)

最新更新