I'm building a chart using bokeh. In this chart, I'm inserting a CheckBoxButtonGroup.
I'm using this code:
LABELS = [" Produzione " + postazione + " " + stazione + " ",
" Cycle time " + postazione + " " + stazione+ " ",
" Scarti " + postazione + " " + stazione+ " ",
" Avarie " + postazione + " " + stazione2+ " ",
" Stati " + postazione + " "]
checkbox_button_group = CheckboxButtonGroup(labels=LABELS, active=[0,0,0,0,0])
With this code, I thought to display the 5 checkbox buttons no flagged.
But if I try to render the page I can see this situation:
As you can see the first option is every time selected. It is possible to display at the first time all options no selected?
You can use the default or pass an empty list to the value active=[] in CheckboxButtonGroup.
CheckboxButtonGroup(labels=LABELS)
# or
CheckboxButtonGroup(labels=LABELS, active=[])
For a better understanding. active=[0] sets the box at possition 0 to active. active=[1,4] sets two boxes to active at possition 1 and 4.
This is the output:
Related
I'm currently using openpyxl to use an excel file that has mul-indices (2 Levels of headers) and i'm trying to do the operations depending on the subheaders a header has.
I have some exp. doing this in pandas but for this Project i have to use openpyxl which i barly made any use of before.
Only thing i could think off is the manual way:
iterating over the rows
saving the first row as header and 2nd row as subheader
do some cleaning.
manually save the headers with their subheaders in dics. then filleing in the values by iterating over all the cols
my code is as follows:
#reading the excel file
path = r'path to file'
wb = load_workbook(path) #loading the excel table
ws = wb.active #grab the active worksheet
#Setting the doc Header
for h in ws.iter_rows(max_row = 1, values_only = True): #getting the first row (Headers) in the table
header = list(h)
for sh in ws.iter_rows(min_row = 1 ,max_row = 2, values_only = True):
sub_header = list(sh)
#removing all of the none Values
header = list(filter(None, header))
sub_header = list(filter(None, sub_header))
print(header)
print(sub_header)
#creating a list of all the Columns in the excel file
col_list = []
for col in ws.iter_cols(min_row=3,min_col = 1): #Iteration over every single row starting from the third row since first two are the headers
col = [cell.value for cell in col] #Creating a list from each row
col = list(filter(None, col)) #removing the none values from each row
col_list.append(col) #creating a list of all rows (starting from the 3d one)
#print (col_list)
But i'm sure there must be a better way that i wasnt able to find in the docs or by checking this website.
Thanks in advance!
My goal in the end is to automate this part of my code by iterating over the header and use the subheaders of that head and their values each time
code:
#bulding the templates using yattag "yattag.org"
doc , tag , text = Doc().tagtext()
#building the tags of the xml file
with tag("Data"): #root tag
for row in row_list :
with tag("Row"):
with tag("Input"):
with tag(header[0].replace(' ','_').replace('\n','_')):
text("In " + dic[row[0]]+" the precentage of Students " + " regarding the " + header[0] + " the Precentage of Students with "+ sub_header[0] + " is "+ str(row[1]) + " whereas the " + sub_header[1] + " are " + str(row[2]) )
with tag("Row_Data"):
text(dic[row[0]] + " | " + header[0] + " | " + sub_header[0]+ " | " + str(row[1]) + " | " + sub_header[1] + " | " + str(row[2]))
with tag(header[1].replace(' ','_').replace('\n','_')):
text("In " + dic[row[0]]+" the precentage of Students " + " regarding the " + header[1] + " the Precentage of Students with "+ sub_header[2] + " is "+ str(row[3]) + " whereas the " + sub_header[3] + " are " + str(row[4]) )
with tag("Row_Data"):
text(dic[row[0]] + " | " + header[1] + " | " + sub_header[2]+ " | " + str(row[3]) + " | " + sub_header[3] + " | " + str(row[4]))
with tag(header[2].replace(' ','_').replace('\n','_')):
text("In " + dic[row[0]]+" the precentage of Students " + " regarding the " + header[2] + " the Precentage of Students with "+ sub_header[4] + " is "+ str(row[5]) + " whereas the " + sub_header[5] + " are " + str(row[6]) )
with tag("Row_Data"):
text(dic[row[0]] + " | " + header[2] + " | " + sub_header[4]+ " | " + str(row[5]) + " | " + sub_header[5] + " | " + str(row[6]))
with tag(header[3].replace(' ','_').replace('\n','_')):
text("In " + dic[row[0]]+" the precentage of Students " + " regarding the " + header[3] + " the Precentage of Students with "+ sub_header[6] + " is "+ str(row[7]) + " whereas the " + sub_header[7] + " are " + str(row[8]) +" and for " + sub_header[8] + str(row[9]) )
with tag("Row_Data"):
text(dic[row[0]] + " | " + header[3] + " | " + sub_header[6]+ " | " + str(row[7]) + " | " + sub_header[7] + " | " + str(row[8]) + " | " + sub_header[8] + " | " + str(row[9]))
with tag(header[4].replace(' ','_').replace('\n','_')):
text("In " + dic[row[0]]+" the precentage of Students " + " regarding the " + header[4] + " the Precentage of Students with "+ sub_header[9] + " is "+ str(row[10]) + " whereas the " + sub_header[10] + " are " + str(row[11]) )
with tag("Row_Data"):
text(dic[row[0]] + " | " + header[4] + " | " + sub_header[9]+ " | " + str(row[10]) + " | " + sub_header[10] + " | " + str(row[11]))
with tag(header[5].replace(' ','_').replace('\n','_')):
text("In " + dic[row[0]]+" the precentage of Students " + " regarding the " + header[5] + " the Precentage of Students with "+ sub_header[11] + " is "+ str(row[12]) + " whereas the " + sub_header[12] + " are " + str(row[13]) )
with tag("Row_Data"):
text(dic[row[0]] + " | " + header[5] + " | " + sub_header[11]+ " | " + str(row[12]) + " | " + sub_header[12] + " | " + str(row[13]))
with tag(header[6].replace(' ','_').replace('\n','_')):
text("In " + dic[row[0]]+" the precentage of Students " + " regarding the " + header[6] + " the Precentage of Students with "+ sub_header[13] + " is "+ str(row[14]) + " whereas the " + sub_header[14] + " are " + str(row[15]) )
with tag("Row_Data"):
text(dic[row[0]] + " | " + header[6] + " | " + sub_header[13]+ " | " + str(row[14]) + " | " + sub_header[14] + " | " + str(row[15]))
#print(doc.getvalue())
result = indent(
doc.getvalue(),
indentation=' ',
indent_text=True
)
#saving the xml file
with open("output.xml", "w") as f:
f.write(result)
Unless Pandas is completely off the table I think you might be able to do something pandas and openpyxl. The documentation mentions reading data from openpyxl into a pandas dataframe: Working with Pandas and Numpy.
Could you use:
data = ws.values
df = DataFrame(data[2:,:], index=data[0], columns=data[1])
There may be some filtering necessary with regards to the None values.
I have code writing data to 3 different files. I need to add sales tax to the price output of f3 that will change the price value. what would be the cleanest way to do this?
Edit: price structure is (00.00)
# write compiled product to buylist.txt
def write_output(self):
with open(self.outputfile, 'w') as f, open(self.outputfile2, 'w') as f2, open(self.outputfile3, 'w') as f3:
for item in self.buylist:
f.write(str(item['status']) + " " + str(item['item']) + " " + str(item['quantity'])+ " " + str(item['price']) + "\n")
f2.write(str(item['item']) + " " + str(item['quantity']) + "\n")
f3.write(str(item['item']) + " " + str(item['quantity'])+ " " + str(item['price']) + "\n")
f.close(), f2.close(), f3.close()
You can use this code, Although this is not a very good way to ask a question since you have not mentioned data type of price or given an example.
You will only need to edit the f3.write line
salesTax = 18
f3.write(str(item['item']) + " " + str(item['quantity'])+ " " + str(int(item['price']) * (1 + (salesTax/100))) + "\n")
I keep getting the above mentioned error when running:
def Decode(iList):
IssuerList = ["Dummy","enRoute","JCB","Diner's Club","Visa"
,"Master Card","Union Pay","Petroleum"]
TypeList = ["Debit account", "Credit account"]
for istr in iList:
ostr = istr + ": Was issued by " + IssuerList[int(istr[1])] + " in 20"
+ istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8]
+ ". The card is linked to a " + TypeList[int(istr[8])]
+ " with the account number: " + istr[8:]
WriteFile(ostr)
File "", line 24, in Decode
+ istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8]
TypeError: bad operand type for unary +: 'str'
Have tried str() on the bad line but no luck.
You have a valid, complete line of Python
ostr = istr + ": Was issued by " + IssuerList[int(istr[1])] + " in 20"
Then another line starting with a unary +, which isn't a valid statement applied to a string.
+ istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8]
See How can I do a line continuation in Python for the options to continue an expression over more than one line.
The practice recommended in the style guide is to use parenthesis and break before the operator.
def Decode(iList):
IssuerList = ["Dummy","enRoute","JCB","Diner's Club","Visa"
,"Master Card","Union Pay","Petroleum"]
TypeList = ["Debit account", "Credit account"]
for istr in iList:
ostr = (istr + ": Was issued by " + IssuerList[int(istr[1])] + " in 20"
+ istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8]
+ ". The card is linked to a " + TypeList[int(istr[8])]
+ " with the account number: " + istr[8:])
WriteFile(ostr)
You are assuming that python knows that you want to carry the expression to the next line. In this case it does not know that unless you tell it using a \ at the end of the line or enclose the expression in (). Basically some way to indicate that the seemingly completed expression is not complete and continues on the next line.
This:
ostr = istr + ": Was issued by " + IssuerList[int(istr[1])] + " in 20"
+ istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8]
+ ". The card is linked to a " + TypeList[int(istr[8])]
+ " with the account number: " + istr[8:]
should be:
ostr = istr + ": Was issued by " + IssuerList[int(istr[1])] + " in 20" \
+ istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8] \
+ ". The card is linked to a " + TypeList[int(istr[8])] \
+ " with the account number: " + istr[8:]
or:
ostr = (istr + ": Was issued by " + IssuerList[int(istr[1])] + " in 20"
+ istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8]
+ ". The card is linked to a " + TypeList[int(istr[8])]
+ " with the account number: " + istr[8:])
Now let me explain the actual error you are getting 'TypeError: bad operand type for unary +: 'str' in string creation' is because it has finished parsing the seemingly complete first line or the part clipped out above and has now moved on to parse the second line. It sees the new line starts with the + so it must be the unary + since there is no operand on the left of the +. It is not applicable to the string operand that comes next, so it complains.
Adding backslashes when creating string on multiple lines, should solve the problem:
ostr = istr + ": Was issued by " + IssuerList[int(istr[1])] + " in 20" \
+ istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8] \
+ ". The card is linked to a " + TypeList[int(istr[8])] \
+ " with the account number: " + istr[8:]
This is my file text:
Covid-19 Data
Country / Number of infections / Number of Death
USA 124.356 2.236
Netherlands 10.866 771
Georgia 90 NA
Germany 58.247 455
I created a function to calculate the ratio of deaths compared to the infections, however it does not work, because some of the values aren't floats.
f=open("myfile.txt","w+")
x="USA" + " " + " " + "124.356" + " " + " " + "2.236"
y="Netherlands" + " " + " " + "10.866" + " " + " " + "771"
z="Georgia" + " " + " " + "90" + " " + " " + "NA"
w="Germany" + " " + " " + "58.247" + " " + " " + "455"
f.write("Covid-19 Data" + "\n" + "Country" + " " + "/" + " " + "Number of infections" + " " + "/" + " " + "Number of Death" + "\n")
f.write(x + "\n")
f.write(y + "\n")
f.write(z + "\n")
f.write(w)
f.close()
with open("myfile.txt", "r") as file:
try:
for i in file:
t = i.split()
result=float(t[-1])/float(t[-2])
print(results)
except:
print("fail")
file.close()
Does someone have an idea how to solve this problem ?
You can do the following:
with open("myfile.txt", "r") as file:
for i in file:
t = i.split()
try:
result = float(t[-1]) / float(t[-2])
print(result)
except ValueError:
pass
At the time you don't know if the values you are trying to divide are numeric values or not, therefore surrounding the operation with a try-catch should solve your problem.
If you want to become a bit more "clean" you can do the following:
def is_float(value):
try:
float(value)
except ValueError:
return False
return True
with open("myfile.txt", "r") as file:
for i in file:
t = i.split()
if is_float(t[-1]) and is_float(t[-2]):
result = float(t[-1]) / float(t[-2])
print(result)
The idea is the same, however.
I used the same file that you attached in your example. I created this function hopefully it helps:
with open("test.txt","r") as reader:
lines = reader.readlines()
for line in lines[2:]:
line = line.replace(".","") # Remove points to have the full value
country, number_infections, number_deaths = line.strip().split()
try:
number_infections = float(number_infections)
number_deaths = float(number_deaths)
except Exception as e:
print(f"[WARNING] Could not convert Number of Infections {number_infections} or Number of Deaths {number_deaths} to float for Country: {country}\n")
continue
ratio = number_deaths/number_infections
print(f"Country: {country} D/I ratio: {ratio}")
As you can see I avoided the headers of your file using lines[2:] that means that I will start from row 3 of your file. Also, added try/exception logic to avoid non-float converts. Hope this helps!
Edit
Just noticed that the format for thousands is used with "." instead "," in that case the period was removed in line 7.
The results for this execution is:
Country: USA D/I ratio: 0.017980636237897647
Country: Netherlands D/I ratio: 0.07095527332965212
[WARNING] Could not convert Number of Infections 90.0 or Number of Deaths NA to float for Country: Georgia
Country: Germany D/I ratio: 0.007811561110443456
Fixed the following:
The first two lines in your text-file are headers. These need to be skipped
'NA' Can't be converted to zero
If there is a 0 in your data, your program would crash. Now it wouldn't.
f=open("myfile.txt","w+")
x="USA" + " " + " " + "124.356" + " " + " " + "2.236"
y="Netherlands" + " " + " " + "10.866" + " " + " " + "771"
z="Georgia" + " " + " " + "90" + " " + " " + "NA"
w="Germany" + " " + " " + "58.247" + " " + " " + "455"
f.write("Covid-19 Data" + "\n" + "Country" + " " + "/" + " " + "Number of infections" + " " + "/" + " " + "Number of Death" + "\n")
f.write(x + "\n")
f.write(y + "\n")
f.write(z + "\n")
f.write(w)
f.close()
with open("myfile.txt", "r") as file:
#Skipping headers
next(file)
next(file)
try:
for i in file:
t = i.split()
#Make sure your code keeps working when one of the numbers is zero
x = 0
y = 0
#There are some NA's in your file. Strings not representing
#a number can't be converted to float
if t[1] != "NA":
x = t[1]
if t[2] != "NA":
y = t[2]
if x == 0 or y == 0:
result = 0
else:
result=float(x)/float(y)
print(t[0] + ": " + str(result))
except:
print("fail")
file.close()
Output:
USA: 55.615384615384606
Netherlands: 0.014093385214007782
Georgia: 0
Germany: 0.12801538461538461
Your header line in the file is Covid-19 Data. this is the first line and when you call t=i.split() you then have a list t which has data ['Covid-19', 'Data']
you cannot convert these to floats since they have letters in them. Instead you should read the first 2 header line before the loop and do nothing with them. However you are then going to have issues with Georgia as "NA" also cannot be converted to a float.
A few other points, its not good practice to have a catch all exception. Also you dont need to close the file explicitly if you open the file using a with statement.
I'm relatively new to D3 and am still trying to wrap my head around how the data sets link together. For simplicity sake, I am using this beautiful chord chart by Mike Bostock as my base design.
I've created another 35x35 matrix of fake data, a variable defined as "corr":
In Python
corr = pandas.DataFrame(randn(35,35))
Placed in the script
var corr = [[...],
[...],
[...],
.
.
.,
[...]];
In the script, the title chunk is:
chord.append("title").text(function(d) {
return " " + cities[d.target.index].name
+ " → " + cities[d.source.index].name
+ ": " + formatPercent(d.source.value)
+ "\n " + cities[d.source.index].name
+ " → " + cities[d.target.index].name
+ ": " + formatPercent(d.target.value);
});
I think my problem is that I am trying to call from a different data set, but regardless, all of my approaches end in the chord chart or titles not appearing.
Any and all advice is appreciated!
What are you defining 'cities' as? The example loads this csv into an array of objects:
d3.csv("cities.csv", function(cities) {
Since you don't have any city names, the function that creates the title text crashes instead of returning creating a title.
I would recommend just using the index number in the titles:
chord.append("title").text(function(d) {
return " " + d.target.index
+ " → " + d.source.index
+ ": " + formatPercent(d.source.value)
+ "\n " + d.source.index
+ " → " + d.target.index
+ ": " + formatPercent(d.target.value);
});
if you can't get that to work, you should post (http://bl.ocks.org/ or http://jsfiddle.net/) what you've done so far; it is hard to guess what might be going wrong.