python脚本中断输出行

2024-04-16 15:04:22 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我跑

import numpy as np
import pandas as pd
import sys

df = pd.read_csv(sys.argv[1]) # note to self: argv[0] is script file content

description = df.groupby(['option','subcase']).describe()
totals = df.groupby('option').describe().set_index(np.array(['total'] * df['option'].nunique()), append=True)
description = description.append(totals).sort_index()
print(description)

关于.csv

option,subcase,cost,time
A,sub1,4,3
A,sub1,2,0
A,sub2,3,8
A,sub2,1,2
B,sub1,13,0
B,sub1,11,0
B,sub2,5,2
B,sub2,3,4

,我得到如下输出:

                cost                                                  time  \
               count  mean       std   min    25%   50%    75%   max count   
option subcase                                                               
A      sub1      2.0   3.0  1.414214   2.0   2.50   3.0   3.50   4.0   2.0   
       sub2      2.0   2.0  1.414214   1.0   1.50   2.0   2.50   3.0   2.0   
       total     4.0   2.5  1.290994   1.0   1.75   2.5   3.25   4.0   4.0   
B      sub1      2.0  12.0  1.414214  11.0  11.50  12.0  12.50  13.0   2.0   
       sub2      2.0   4.0  1.414214   3.0   3.50   4.0   4.50   5.0   2.0   
       total     4.0   8.0  4.760952   3.0   4.50   8.0  11.50  13.0   4.0   


                mean       std  min   25%  50%   75%  max  
option subcase                                             
A      sub1     1.50  2.121320  0.0  0.75  1.5  2.25  3.0  
       sub2     5.00  4.242641  2.0  3.50  5.0  6.50  8.0  
       total    3.25  3.403430  0.0  1.50  2.5  4.25  8.0  
B      sub1     0.00  0.000000  0.0  0.00  0.0  0.00  0.0  
       sub2     3.00  1.414214  2.0  2.50  3.0  3.50  4.0  
       total    1.50  1.914854  0.0  0.00  1.0  2.50  4.0  

这很烦人,尤其是如果您想将其保存为.csv,而不是在控制台中显示它

(例如python myscript.py my.csv > my.summary

我如何阻止这种断线的发生


Tags: csvimportdfasnpsysdescriptiontotal
1条回答
网友
1楼 · 发布于 2024-04-16 15:04:22

添加:^{}

pd.set_option('expand_frame_repr', False)

print(description)
                cost                                                  time                                           
               count  mean       std   min    25%   50%    75%   max count  mean       std  min   25%  50%   75%  max
option subcase                                                                                                       
A      sub1      2.0   3.0  1.414214   2.0   2.50   3.0   3.50   4.0   2.0  1.50  2.121320  0.0  0.75  1.5  2.25  3.0
       sub2      2.0   2.0  1.414214   1.0   1.50   2.0   2.50   3.0   2.0  5.00  4.242641  2.0  3.50  5.0  6.50  8.0
       total     4.0   2.5  1.290994   1.0   1.75   2.5   3.25   4.0   4.0  3.25  3.403430  0.0  1.50  2.5  4.25  8.0
B      sub1      2.0  12.0  1.414214  11.0  11.50  12.0  12.50  13.0   2.0  0.00  0.000000  0.0  0.00  0.0  0.00  0.0
       sub2      2.0   4.0  1.414214   3.0   3.50   4.0   4.50   5.0   2.0  3.00  1.414214  2.0  2.50  3.0  3.50  4.0
       total     4.0   8.0  4.760952   3.0   4.50   8.0  11.50  13.0   4.0  1.50  1.914854  0.0  0.00  1.0  2.50  4.0

相关问题 更多 >