Python help! I am writing a program that reads an excel file to answer some ques
ID: 3710233 • Letter: P
Question
Python help!
I am writing a program that reads an excel file to answer some questions. In the data sheet (attached image) each line shows the XRD data of all the materials given a specific Intensity (Arb. Units). AlNi* are the name of the materials. Each column shows the XRD data of one material. I have written the code to count the materials but am trying to find a way to not include intensity when it counts the materials as well as the other questions listed if possible! I attached an screen shot of the data excel page and of the code i have written now that i need to modify to not include the intensity for #1.
1. How many materials the data has?
2. How many missing values the data has? (missing value is represented as ‘ ’)
3. How many different Intensity the data has?
1 file-open("4material.csv", "r") 2 lines-file. readlines () # print (lines) 4 5#1 6 attributes-lines [0].strip).split(",") 7 mapA2Index-h 8 index-0 9 for x in attributes: mapA2Index [x.strip()]-index index+-1 10 12 print("How many materials the data has:", index)Explanation / Answer
#Updated and Appended the solutions to your code provided.
file=open("4material.csv","r")
lines = file.readlines()
#1 -- Updated your code a bit
attributes = lines[0].strip().split(",")
mapA2Index={}
index = 0
for x in attributes[1:]:
mapA2Index[x.strip()]=index
index+=1
#2 -- This gives the count for : how many missing values the data has
#3 -- Not sure if you wanted only the count of intensity (number of columns). If you want to add the data of intensity column, then plz uncomment the commented #lines. Else remove those commented lines for intensitySum.
spaceCount=0
intensityCount=0
#intensitySum = 0
for line in lines[1:]:
intensityCount+=1
materials = line.strip().split(",")
# val = materials[0]
# intensitySum +=(int)(val)
for data in materials[1:]:
check=data.strip()
if check.isspace():
spaceCount+=1
elif len(check.split())==0:
spaceCount+=1
print("How many materials the data has : ",index)
print("How many missing values the data has : ",spaceCount)
print("How many different Intensity the data has: ",intensityCount)
#print("How many different Intensity the data has: ",intensitySum)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.