Python Forum
How to calculated how many fail in each site(s) in csv files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to calculated how many fail in each site(s) in csv files
#1
Hi all,

I just get how many test quantity for each site(s),and then want to calculated each site pass quantity. I have no idea to do that,someone can help me for that, thanks in advance!

import pandas as pd

df=pd.DataFrame(pd.read_csv('111.csv',skiprows=[0,2,3]))

df_dict1=df.to_dict('list')['Site Number']

for i in range(0,35):
    print("site({})testing Qty is:{}.".format(i,df_dict1.count(i)))


???????????????????????????????????????????????????????????????????
df_dict2=df.set_index('Site Number').to_dict('list')['Pass/Fail']

for i in range(0,35):
    print("site({})testing Pass Qty is:{}.".format(i,????))
???????????????????????????????????????????????????????????????????
Reply
#2
I have tried stupid method to handle this issue, could someone use simple to do that ,thanks!!

import pandas as pd

df=pd.DataFrame(pd.read_csv('111.csv',skiprows=[0,2,3]))

df_dict1=df.to_dict('list')['Site Number']

for i in range(0,35):
    print("site({})testing Qty is:{}.".format(i,df_dict1.count(i)))

df_dict2=df.set_index('Site Number').to_dict('list')['Pass/Fail']
df_dict4=df.set_index('Pass/Fail').to_dict('list')['Site Number']
df_dict3=dict (zip(df['Site Number'], df['Pass/Fail']))

tt=zip(df_dict4,df_dict2)


site0_failed_total=site1_failed_total=site2_failed_total=site3_failed_total=site4_failed_total=site5_failed_total=site6_failed_total=site7_failed_total=\
site8_failed_total=site9_failed_total=site10_failed_total=site11_failed_total=site12_failed_total=site13_failed_total=site14_failed_total=site15_failed_total=\
site16_failed_total=site17_failed_total=site18_failed_total=site19_failed_total=site20_failed_total=site21_failed_total=site22_failed_total=site23_failed_total=\
site24_failed_total=site25_failed_total=site26_failed_total=site27_failed_total=site28_failed_total=site29_failed_total=site30_failed_total=site31_failed_total=\
site32_failed_total=site33_failed_total=site34_failed_total=0

for j,k in tt:
    if j==0 and k=='Failed':
        site0_failed_total+=1
    elif j==1 and k=='Failed':
        site1_failed_total += 1
    elif j==2 and k=='Failed':
        site2_failed_total += 1
    elif j==3 and k=='Failed':
        site3_failed_total += 1
    elif j==4 and k=='Failed':
        site4_failed_total += 1
    elif j==5 and k=='Failed':
        site5_failed_total += 1
    elif j==6 and k=='Failed':
        site6_failed_total += 1
    elif j==7 and k=='Failed':
        site7_failed_total += 1
    elif j==8 and k=='Failed':
        site8_failed_total += 1
    elif j==9 and k=='Failed':
        site9_failed_total += 1
    elif j==10 and k=='Failed':
        site10_failed_total += 1
    elif j==11 and k=='Failed':
        site11_failed_total += 1
    elif j==12 and k=='Failed':
        site12_failed_total += 1
    elif j==13 and k=='Failed':
        site13_failed_total += 1
    elif j==14 and k=='Failed':
        site14_failed_total += 1
    elif j==15 and k=='Failed':
        site15_failed_total += 1
    elif j==16 and k=='Failed':
        site16_failed_total += 1
    elif j==17 and k=='Failed':
        site17_failed_total += 1
    elif j==18 and k=='Failed':
        site18_failed_total += 1
    elif j==19 and k=='Failed':
        site19_failed_total += 1
    elif j==20 and k=='Failed':
        site20_failed_total += 1
    elif j==21 and k=='Failed':
        site21_failed_total += 1
    elif j==22 and k=='Failed':
        site22_failed_total += 1
    elif j==23 and k=='Failed':
        site23_failed_total += 1
    elif j==24 and k=='Failed':
        site24_failed_total += 1
    elif j==25 and k=='Failed':
        site25_failed_total += 1
    elif j==26 and k=='Failed':
        site26_failed_total += 1
    elif j==27 and k=='Failed':
        site27_failed_total += 1
    elif j==28 and k=='Failed':
        site28_failed_total += 1
    elif j==29 and k=='Failed':
        site29_failed_total += 1
    elif j==30 and k=='Failed':
        site30_failed_total += 1
    elif j==31 and k=='Failed':
        site31_failed_total += 1
    elif j==32 and k=='Failed':
        site32_failed_total += 1
    elif j==33 and k=='Failed':
        site33_failed_total += 1
    elif j==34 and k=='Failed':
        site34_failed_total += 1

print("Site0 Failed QTY:",site0_failed_total)
print("Site1 Failed QTY:",site1_failed_total)
print("Site2 Failed QTY:",site2_failed_total)
print("Site3 Failed QTY:",site3_failed_total)
print("Site4 Failed QTY:",site4_failed_total)
print("Site5 Failed QTY:",site5_failed_total)
print("Site6 Failed QTY:",site6_failed_total)
print("Site7 Failed QTY:",site7_failed_total)
print("Site8 Failed QTY:",site8_failed_total)
print("Site9 Failed QTY:",site9_failed_total)
print("Site10 Failed QTY:",site10_failed_total)
print("Site11 Failed QTY:",site11_failed_total)
print("Site12 Failed QTY:",site12_failed_total)
print("Site13 Failed QTY:",site13_failed_total)
print("Site14 Failed QTY:",site14_failed_total)
print("Site15 Failed QTY:",site15_failed_total)
print("Site16 Failed QTY:",site16_failed_total)
print("Site17 Failed QTY:",site17_failed_total)
print("Site18 Failed QTY:",site18_failed_total)
print("Site19 Failed QTY:",site19_failed_total)
print("Site20 Failed QTY:",site20_failed_total)
print("Site21 Failed QTY:",site21_failed_total)
print("Site22 Failed QTY:",site22_failed_total)
print("Site23 Failed QTY:",site23_failed_total)
print("Site24 Failed QTY:",site24_failed_total)
print("Site25 Failed QTY:",site25_failed_total)
print("Site26 Failed QTY:",site26_failed_total)
print("Site27 Failed QTY:",site27_failed_total)
print("Site28 Failed QTY:",site28_failed_total)
print("Site29 Failed QTY:",site29_failed_total)
print("Site30 Failed QTY:",site30_failed_total)
print("Site31 Failed QTY:",site31_failed_total)
print("Site32 Failed QTY:",site32_failed_total)
print("Site33 Failed QTY:",site33_failed_total)
print("Site34 Failed QTY:",site34_failed_total)
Reply
#3
I would argue that if you are more specific in defining what you would like to accomplish then (a) you probably solve it by yourself (b) others can help you.

As it written now (incl. the code) I don't understand what is the objective.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
If tt is really a dictionary of site:status key value pairs, you can count fails like this:
failed = {}
for site, pf in tt:
    if pf == "Failed":
        failed[site] = failed.get(site, 0) + 1
    elif site not in failed:
        failed[site] = 0

for site, count in failed:
    print(f"Site {site} Failed QTY: {count}")
If you don't care about sites with no failures, you can leave out the elif part.

This is a way to do the same thing without making the pass/vail dictionary.
import pandas as pd
import numpy as np
import random

# Make some random data to work with.
count = 10
df = pd.DataFrame({
    "Site Number":[random.randint(1, 6) for _ in range(count)],
    "Pass/Fail":[random.choice(["Pass", "Fail"]) for _ in range(count)]})

# Create new colum that has 1 for fail, 0 for pass.  Group rows by the site number and add up the fail column.
df["Fail"] = np.where(df['Pass/Fail'] == "Fail", 1, 0)
print(df.groupby(["Site Number"]).sum())
             Fail
Site Number
1               0
2               2
3               1
5               1
If you want a failure rate instead of count.
import pandas as pd
import numpy as np
import random

count = 10
df = pd.DataFrame({
    "Site Number":[random.randint(1, 6) for _ in range(count)],
    "Pass/Fail":[random.choice(["Pass", "Fail"]) for _ in range(count)]})

# Like above, but set fail to 100 and pass to 0.  Group and get average of fail column.
df["Fail"] = np.where(df['Pass/Fail'] == "Fail", 100, 0)
print(df.groupby(["Site Number"]).mean())
Output:
Fail Site Number 2 0.000000 3 66.666667 4 66.666667 5 50.000000 6 100.000000
But perfringo is right, you need to do a better job describing what it is you are trying to accomplish. I solve problems using pencil in paper, writing down each of the steps. It is easier to write code if I already know what the program is supposed to do. It lets me focus on the coding because the design work is mostly done.
Reply
#5
Much apricated for that. The code purpose is get each site(s) from(site1-site35)failure rate, I don't know what mean of the last code.
df = pd.DataFrame(pd.read_csv("111.csv",skiprows=[0,2,3],low_memory=False))
dict1 = df.to_dict('list')['Site Number']
sites={}
for i in dict1:
    sites[i]=sites.get(i,0)+1   # get each site(s) total quantity


dict2=studentDf.to_dict('list')['Site Number']
dict3=studentDf.to_dict('list')['Pass/Fail']
tt=zip(dict2,dict3)
failed = {}
for site, pf in tt:
    if pf == "Failed":
        failed[site] = failed.get(site, 0) + 1
    elif site not in failed:
        failed[site] = 0

for site, count in failed.items():
    print(f"Site {site} Failed QTY: {count}") # get each site(s) total Failed.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why does [root.destroy, exit()]) fail after pyinstaller? Rpi Edward_ 4 584 Oct-18-2023, 11:09 PM
Last Post: Edward_
  Calculated DF column from dictionary value plantagenet 2 837 Sep-16-2022, 12:46 AM
Last Post: plantagenet
  Imports that work with Python 3.8 fail with 3.9 and 3.10 4slam 1 2,550 Mar-11-2022, 01:50 PM
Last Post: snippsat
  [SOLVED] Why does regex fail cleaning line? Winfried 5 2,410 Aug-22-2021, 06:59 PM
Last Post: Winfried
  scraping video src fail jacklee26 5 3,426 Jul-11-2021, 09:38 AM
Last Post: snippsat
  fail to upgrade pygobject jiapei100 0 3,048 Aug-16-2018, 10:32 AM
Last Post: jiapei100
  How do I print a returned variable calculated in another function? RedSkeleton007 3 3,493 Jul-10-2018, 12:10 PM
Last Post: buran
  Fail to allocate bitmap rsbeesh 7 15,582 Dec-13-2017, 01:36 PM
Last Post: wavic
  Sorting values calculated in python stumunro 4 3,901 Sep-13-2017, 06:09 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020