Warm tip: This article is reproduced from serverfault.com, please click

python-如何获取Pandas数据透视表中的列数和每列的宽度?

(python - How to get the number of columns and the width of each column in a Pandas pivot table?)

发布于 2020-11-28 00:00:28

我希望能够获取Pandas数据透视表中的列数和每列的宽度。这是我的代码:

import pandas as pd
import numpy as np
df = pd.DataFrame({'System_Key':['MER-002', 'MER-003', 'MER-004', 'MER-005', 'MER-006', 'MER-007', 'BAV-378', 'BAV-379', 'BAV-380', 'BAV-381', 'AUD-220', 'AUD-221', 'AUD-222', 'AUD-223'],
                       'Manufacturer':['Mercedes', 'Mercedes', 'Mercedes', 'Mercedes', 'Mercedes', 'Mercedes', 'BMW', 'BMW', 'BMW', 'BMW', 'Audi', 'Audi', 'Audi', 'Audi'],
                       'Region':['Americas', 'Europe', 'Americas', 'Asia', 'Asia', 'Europe', 'Europe', 'Asia', 'Europe', 'Europe', 'Americas', 'Asia', 'Americas', 'Americas'],
                       'Department':[np.nan, 'Sales', np.nan, 'Operations', np.nan, np.nan, 'Accounting', 'Finance', 'Finance', np.nan, 'Finance', 'Finance', 'Finance', np.nan],
                       'Approver':[np.nan, 'Jones, T.', 'Smith, W.', 'Jones, T.', 'Jones, T.', np.nan, np.nan, np.nan, 'Balakrishnan, G.', np.nan, np.nan, np.nan, np.nan, np.nan]
                      })

df = df.applymap(str)

df['Rebate_Plan'] = np.where(df['System_Key'].str.contains('BAV', na=False), 'Jupyter',
                    np.where(df['System_Key'].str.contains('AUD', na=False), 'Uranus',
                    np.where((df['System_Key'].str.contains('MER', na=False)) & (df['Approver'].str.contains('Jones', na=False)), 'Saturn',
                    np.where((df['System_Key'].str.contains('MER')) & (~df['Approver'].str.contains('Jones')), 'Pluto', '*No Plan*'))))

df.replace(['None', 'nan'], np.nan, inplace=True)

pivot_data = pd.pivot_table(data=df, index='Manufacturer', columns='Approver', values='System_Key', aggfunc='count', margins=True, margins_name='TOTALS', fill_value=0)

结果如下

print('-' * 60)
print(pivot_data)
print('-' * 60)

----------------------------------------------------------------------
Approver            Balakrishnan, G.    Jones, T.   Smith, W.   TOTALS
Manufacturer                
BMW                 1                   0           0           1
Mercedes            0                   3           1           4
TOTALS              1                   3           1           5
----------------------------------------------------------------------

在此示例中,请注意,在数据透视表上方和下方的连字符(-)换行符终止于“ TOTALS”的最后一个字符。

我希望能够获得数量和宽度,以便可以在控制台中的数据透视表的上方和下方打印可变长度的换行符

如果我们不考虑列宽,那么列数的伪代码将如下所示:

if number_of_pivot_table_columns == 2:
    print('-' * 45)
    print(pivot_data)
    print('-' * 45)

elif number_of_pivot_table_columns == 3:
    print('-' * 60)
    print(pivot_data)
    print('-' * 60)

elif number_of_pivot_table_columns == 4:
    print('-' * 75)
    print(pivot_data)
    print('-' * 75)

(当然,这种方法仅在列宽为15个字符的情况下才有效,在实践中不会发生这种情况,因为此人的名字的姓氏和名字可以是任意数量的字符。)

在此先感谢你的帮助!

Questioner
equanimity
Viewed
33
frankr6591 2020-11-28 08:38:24

使用str()来找到最大len。

outStr = str(pivot_data)
mxLen = max([len(l) for l in outStr.split('\n')])
print('-'*mxLen)
print(outStr)
print('-'*mxLen)