[英]Calculating a summary row in a pandas dataframe
Here's what I have (Pandas DataFrame), that I created so far: Code:这是我迄今为止创建的(Pandas DataFrame):代码:
table = pd.pivot_table(df1, index=['Assignee', 'IssueType'], columns=['Status'], values='Key', aggfunc={'Key': np.count_nonzero}, dropna=True)
table['Total'] = table.sum(axis=1)
table = table.fillna(0)
table = table.apply(pd.to_numeric, errors='ignore')
table = table.astype(int)
table.to_csv(output_file_path, sep=delimiter)
Output:输出:
Assignee~IssueType~Analysis~Blocked~Closed~Done~In Progress~Open~Ready For QA Testing~Total
Smith, John~Story~0~0~0~0~0~1~0~1
Smith, John~Sub-task~0~0~0~0~0~1~0~1
Smith, John~Task~0~0~0~0~2~5~0~7
Doe, Jane~Bug~0~0~0~0~1~0~0~1
Polo, Marco~Bug~0~0~0~0~0~2~0~2
Polo, Marco~Story~0~0~1~0~0~0~0~1
Polo, Marco~Task~1~0~0~0~4~2~0~7
Here's what I would like to have (considering that I could have numeric/non-numeric columns:这是我想要的(考虑到我可以有数字/非数字列:
Assignee~IssueType~Analysis~Blocked~Closed~Done~In Progress~Open~Ready For QA Testing~Total
Smith, John~Story~0~0~0~0~0~1~0~1
Smith, John~Sub-task~0~0~0~0~0~1~0~1
Smith, John~Task~0~0~0~0~2~5~0~7
Doe, Jane~Bug~0~0~0~0~1~0~0~1
Polo, Marco~Bug~0~0~0~0~0~2~0~2
Polo, Marco~Story~0~0~1~0~0~0~0~1
Polo, Marco~Task~1~0~0~0~4~2~0~7
**GrandTotal~GrandTotal~1~0~1~0~7~11~0~20**
What would be the best/optimal way to achieve this using Pandas DataFrames?使用 Pandas DataFrames 实现这一目标的最佳/最佳方法是什么? Appreciate your help in advance.提前感谢您的帮助。
Here's my answer to this question.这是我对这个问题的回答。 Perhaps there is a scope for improvement (but atleast it works to my contentment).也许还有改进的余地(但至少它让我满意)。
def append_summary_total(df_index, file_path, delimiter):
file_path = os.path.abspath(file_path)
delimiter = str(delimiter)
df = pd.read_csv(file_path, sep=delimiter)
sums = df.select_dtypes(pd.np.number).sum().rename('Grand Total')
df.loc['Grand Total'] = df.select_dtypes(pd.np.number).sum()
df = df.fillna("GrandTotal")
df = df.set_index(df_index)
df = df.apply(pd.to_numeric, errors='ignore')
df = df.astype(int)
df.to_csv(file_path, sep=delimiter)
Here's the output: Sample Output这是输出:示例输出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.