【问题标题】:Print side by side .describe() in pandas在熊猫中并排打印 .describe()
【发布时间】:2022-11-03 21:17:09
【问题描述】:

您好,我有两列我正在使用describe() 并且我正在获取他们的统计信息。我有这样的东西

x=pd.Series([1,3,4,6,7])        
y=pd.Series([75,324,234,42])
desk1=x.describe()
desk2=y.describe()

我想在每个类别的下方打印desk1desk2。我正在这样做:

print("desk1 stats",end="\t\t")
print("desk1 stats")
print(desk1,end="\t\t")
print(desk2)

我明白了:

desk1 stats     desk1 stats
count    5.000000
mean     4.200000
std      2.387467
min      1.000000
25%      3.000000
50%      4.000000
75%      6.000000
max      7.000000
dtype: float64      count      4.000000
mean     168.750000
std      133.185022
min       42.000000
25%       66.750000
50%      154.500000
75%      256.500000
max      324.000000
dtype: float64

我想要的输出是这样的:

desk1 stats     desk1 stats
count    5.000000  count      4.000000
mean     4.200000  mean     168.750000
std      2.387467  std      133.185022
min      1.000000  min       42.000000
25%      3.000000  25%       66.750000
50%      4.000000  50%      154.500000
75%      6.000000  75%      256.500000
max      7.000000  max      324.000000
dtype: float64     dtype: float64

我不想创建数据框。任何解决方案? 提前致谢

【问题讨论】:

    标签: python pandas numpy


    【解决方案1】:

    如何使用:

    print(pd.concat([desk1, desk2],
                     keys=['desk1 description', 'desk2 description'],
                     axis=1))
    

    输出:

           desk1 description  desk2 description
    count           5.000000           4.000000
    mean            4.200000         168.750000
    std             2.387467         133.185022
    min             1.000000          42.000000
    25%             3.000000          66.750000
    50%             4.000000         154.500000
    75%             6.000000         256.500000
    max             7.000000         324.000000
    

    对于更纯粹的 python 解决方案:

    desks = [desk1, desk2]
    
    print('
    '.join(map('    '.join, zip(*(d.to_string().split('
    ')
                    for d in desks)))))
    

    输出:

    count    5.000000    count      4.000000
    mean     4.200000    mean     168.750000
    std      2.387467    std      133.185022
    min      1.000000    min       42.000000
    25%      3.000000    25%       66.750000
    50%      4.000000    50%      154.500000
    75%      6.000000    75%      256.500000
    max      7.000000    max      324.000000
    

    【讨论】:

      【解决方案2】:

      您可以在 concat 之后获得值 describe ,注意 count 不会计算 nan

      pd.concat([x,y],axis=1).describe()
      Out[312]: 
                    0           1
      count  5.000000    4.000000
      mean   4.200000  168.750000
      std    2.387467  133.185022
      min    1.000000   42.000000
      25%    3.000000   66.750000
      50%    4.000000  154.500000
      75%    6.000000  256.500000
      max    7.000000  324.000000
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-01
        • 1970-01-01
        相关资源
        最近更新 更多