简体   繁体   English

如何根据其他列的值在列中显示单个名称? Python

[英]How to show individual names in a column according to the value of other column? Python

I have this code where i am sorting out the shortest processing time first.我有这段代码,我首先要整理出最短的处理时间。 I am using a CSV file as a input where i have data columns like "TestCaseName", "Average_processing_time".我使用 CSV 文件作为输入,其中有“TestCaseName”、“Average_processing_time”等数据列。 Using the Average_processing_time column i am sorting the shorting the shortest processing time first.使用 Average_processing_time 列,我首先对缩短最短处理时间进行排序。 Using bubble sort i got the results but i also want testcase name in a separate column parallel to the processing time.使用冒泡排序我得到了结果,但我还希望测试用例名称在与处理时间平行的单独列中。

Currently the testcase names are as per the CSV (alphabetical order).目前,测试用例名称按照 CSV(字母顺序)。 But instead i want the testcase name according to the average processing time.但相反,我想要根据平均处理时间的测试用例名称。 Here is the code i have currently:-这是我目前拥有的代码:-

          pt=[]     #pt stands for processing time

          n=int(df.TestCaseName.count())
          processes=[]
          for i in range(0,n):
                 processes.insert(i,i+1)

          pt=list(map(float, df["ProcessingTime"]))
          for i in range(0,len(pt)-1):  #applying bubble sort to sort process according to their processing time
             for j in range(0,len(pt)-i-1):
                   if(pt[j]>pt[j+1]):
                      temp=pt[j]
                      pt[j]=pt[j+1]
                      pt[j+1]=temp
                      temp=processes[j]
                      processes[j]=processes[j+1]
                      processes[j+1]=temp
             wt=[]    #wt stands for waiting time
             avgwt=0  #average of waiting time
             #tat=[]    #tat stands for turnaround time
             #avgtat=0   #average of total turnaround time
             wt.insert(0,0)
             #tat.insert(0,bt[0])
             for i in range(1,len(pt)):  
                  wt.insert(i,wt[i-1]+pt[i-1])
                  #tat.insert(i,wt[i]+bt[i])
                  avgwt+=wt[i]
                  #avgtat+=tat[i]
             avgwt=float(avgwt)/n
             #avgtat=float(avgtat)/n
             print("\n")
             print("Process\t\t Processing Time\t\t Waiting Time \t\t TestCaseName") #\t\t\t  
             Turn Around Time
             for i in range(0,n):
                  #  print('{} {} {} {}'.format(processes[i], df.TestCaseName[i], bt[i], 
                  wt[i])
                  #txt = "{0}, \t\t{1}, \t\t{2}, \t\t\t{3}".format(processes[i], bt[i], wt[i], 
                  df.TestCaseName[i] )
                  print(str(processes[i])+"\t\t"+str(pt[i])+"\t\t"+str(wt[i])+ 
                  "\t\t"+str(df.TestCaseName[i]))
                  #print(txt)
                  print("\n")
            print("Average Waiting time is: "+str(avgwt))
            #print("Average Turn Arount Time is: "+str(avgtat))

CSV structure & Output i am getting currently Picture CSV 结构和 Output 我目前正在获取图片

Output i want Output 我想要

you can easily create the following dataframe using:您可以使用以下方法轻松创建以下 dataframe:

import pandas as pd
pd.read_csv('Filename')

This yields the dataframe:这产生了 dataframe:

    TestCase_Name   Avg_Proc_Time
  0    A            2
  1    B            6
  2    C            1
  3    D            7  

To sort this frame simply do:要对该框架进行排序,只需执行以下操作:

df.sort_values(by='Avg_Proc_Time')

which produces the desired output产生所需的 output

    TestCase_Name   Avg_Proc_Time
2      C                1
0      A                2
1      B                6
3      D                7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM