Python pandas汇总表图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30263627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:08:43  来源:igfitidea点击:

Python pandas summary table plot

pythonpandasmatplotlib

提问by Clint

Really can't get to grips with how to plot a summary table of a pandas df. I'm sure this is not a case for a pivot table, or maybe a transposed method of displaying the data. Best I could find was : Plot table and display Pandas DataframeMy code attempts are just not getting there:

真的无法掌握如何绘制熊猫 df 的汇总表。我确定这不是数据透视表的情况,也不是显示数据的转置方法。我能找到的最好的是:绘制表格并显示 Pandas 数据框我的代码尝试没有到达那里:

dc = pd.DataFrame({'A' : [1, 2, 3, 4],'B' : [4, 3, 2, 1],'C' : [4, 3, 2, 1]})
data = dc['A'],dc['B'],dc['C']

ax = plt.subplot(111, frame_on=False) 
ax.xaxis.set_visible(False) 
ax.yaxis.set_visible(False)
cols=["A", "B", "C"]
row_labels=[0]
the_table = plt.table(cellText=data,colWidths = [0.5]*len(cols),rowLabels=row_labels, colLabels=cols,cellLoc = 'center', rowLoc = 'center')
plt.show()

All I would like to do, is produce a table plot, with A B C in the first column, and the total and mean in the rows next to them (see below). Any help or guidance would be great...feeling really stupid... (excuse the code example, it doesn't yet have the total and mean yet included...)

我想要做的就是生成一个表格图,第一列是 ABC,旁边的行是总数和平均值(见下文)。任何帮助或指导都会很棒......感觉真的很愚蠢......(请原谅代码示例,它还没有包含总数和平均值......)

   Total   Mean
A     x      x
B     x      x
C     x      x

采纳答案by cphlewis

import pandas as pd
import matplotlib.pyplot as plt

dc = pd.DataFrame({'A' : [1, 2, 3, 4],'B' : [4, 3, 2, 1],'C' : [3, 4, 2, 2]})

plt.plot(dc)
plt.legend(dc.columns)
dcsummary = pd.DataFrame([dc.mean(), dc.sum()],index=['Mean','Total'])

plt.table(cellText=dcsummary.values,colWidths = [0.25]*len(dc.columns),
          rowLabels=dcsummary.index,
          colLabels=dcsummary.columns,
          cellLoc = 'center', rowLoc = 'center',
          loc='top')
fig = plt.gcf()

plt.show()

enter image description here

enter image description here

回答by fgoettel

Does the dataFrame.describe()function helps you? http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.describe.html

dataFrame.describe()功能对您有帮助吗? http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.describe.html

Sorry, not enough points for comments.

抱歉,没有足够的积分来评论。