I would like to know how to be able to assign a name just to a serie of data (using a scatterchart), without getting the same serie's name as title of the chart. I would like to get just the series name as legend and NOT chart title at all.
I realized that autoTitleDeleted needs to have a value of 1.
So, consulting documentation I found that ´chartContainer´ is the class to implement the aforementioned option. Therefore, I import the class and apply it as next:
from openpyxl import Workbook
from openpyxl.chart import (
ScatterChart,
Reference,
Series,
)
from openpyxl.chart.chartspace import ChartContainer
wb = Workbook()
ws = wb.active
rows = [
["Size", "Batch 1", "Batch 2"],
[2, 40, 30],
[3, 40, 25],
[4, 50, 30],
[5, 30, 25],
[6, 25, 35],
[7, 20, 40],
]
for row in rows:
ws.append(row)
chart2 = ScatterChart()
chart2.x_axis.title = "Size"
chart2.y_axis.title = "Percentage"
xvalues = Reference(ws, min_col = 1, min_row = 2, max_row = 7)
values = Reference(ws, min_col = 3, min_row = 1, max_row = 7)
series1 = Series(values, xvalues)
chart2.series.append(series1)
chart2 = ChartContainer(autoTitleDeleted = 1)
ws.add_chart(chart2, "J10")
wb.save("Ex1.xlsx")
However, next error comes up:
`runfile('C:/Users/Administrador/Desktop/Pre-Try/Ex1/Ex1.py', wdir='C:/Users/Administrador/Desktop/Pre-Try/Ex1')
Traceback (most recent call last):
File "", line 1, in
runfile('C:/Users/Administrador/Desktop/Pre-Try/Ex1/Ex1.py', wdir='C:/Users/Administrador/Desktop/Pre-Try/Ex1')
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Administrador/Desktop/Pre-Try/Ex1/Ex1.py", line 46, in
wb.save("Ex1.xlsx")
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\workbook\workbook.py", line 397, in save
save_workbook(self, filename)
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 294, in save_workbook
writer.save()
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 276, in save
self.write_data()
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 76, in write_data
self._write_worksheets()
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 219, in _write_worksheets
self._write_drawing(ws._drawing)
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 142, in _write_drawing
self._archive.writestr(drawing.path[1:], tostring(drawing._write()))
File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\drawing\spreadsheet_drawing.py", line 296, in _write
self._rels.append(rel)
UnboundLocalError: local variable 'rel' referenced before assignment`
I do not really understand this error. If you could help would be grateful!
chart2 = ChartContainer(autoTitleDeleted = 1)
This the problem: you must use openpyxl chart objects so that the library can manage the plumbing between objects: charts are very complicated objects and we try and hide some of the complexity Your idea is correct - to set the value to True – but that won't work like this because a ChartContainer doesn't know how to add itself to the XLSX package.
As a workaround it's probably easiest create a title with an empty string. Otherwise you could submit a PR that allows the mapping of the autoTitleDeleted attribute from chartContainers to charts and vice versa.
Related
I have the data file which looks like this -
And I have another data file which has all the correct country names.
For matching both the files that, I am using below:
import pandas as pd
names_array=[]
ratio_array=[]
def match_names(wrong_names,correct_names):
for row in wrong_names:
x=process.extractOne(row, correct_names)
names_array.append(x[0])
ratio_array.append(x[1])
return names_array,ratio_array
fields = ['name']
#Wrong country names dataset
df=pd.read_csv("wrong-country-names.csv",encoding="ISO-8859-1",sep=';', skipinitialspace=True, usecols= fields )
print(df.dtypes)
wrong_names=df.dropna().values
#Correct country names dataset
choices_df=pd.read_csv("country-names.csv",encoding="ISO-8859-1",sep='\t', skipinitialspace=True)
correct_names=choices_df.values
name_match,ratio_match=match_names(wrong_names,correct_names)
df['correct_country_name']=pd.Series(name_match)
df['country_names_ratio']=pd.Series(ratio_match)
df.to_csv("string_matched_country_names.csv")
print(df[['name','correct_country_name','country_names_ratio']].head(10))
I get the below error:
name object
dtype: object
Traceback (most recent call last):
File "<ipython-input-221-a1fd87d9f661>", line 1, in <module>
runfile('C:/Users/Drashti Bhatt/Desktop/untitled0.py', wdir='C:/Users/Drashti Bhatt/Desktop')
File "C:\Users\Drashti Bhatt\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:\Users\Drashti Bhatt\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Drashti Bhatt/Desktop/untitled0.py", line 27, in <module>
name_match,ratio_match=match_names(wrong_names,correct_names)
File "C:/Users/Drashti Bhatt/Desktop/untitled0.py", line 9, in match_names
x=process.extractOne(row, correct_names)
File "C:\Users\Drashti Bhatt\Anaconda3\lib\site-packages\fuzzywuzzy\process.py", line 220, in extractOne
return max(best_list, key=lambda i: i[1])
File "C:\Users\Drashti Bhatt\Anaconda3\lib\site-packages\fuzzywuzzy\process.py", line 78, in extractWithoutOrder
processed_query = processor(query)
File "C:\Users\Drashti Bhatt\Anaconda3\lib\site-packages\fuzzywuzzy\utils.py", line 95, in full_process
string_out = StringProcessor.replace_non_letters_non_numbers_with_whitespace(s)
File "C:\Users\Drashti Bhatt\Anaconda3\lib\site-packages\fuzzywuzzy\string_processing.py", line 26, in replace_non_letters_non_numbers_with_whitespace
return cls.regex.sub(" ", a_string)
TypeError: expected string or bytes-like object
I tried with .decode option, but it did not work out. What I am doing wrong?
Any help on this will be much appreciated! Thanks much!
The below code is working. you can find the differences. But i am not sure if this is solution that you are looking. And i have tried on sample files which i had created manually. I have removed fields from pd.read_csv.
(... = same as your code)
...
def match_names(wrong_names,correct_names):
for row in wrong_names:
print('row=',row)
...
return names_array,ratio_array
fields = ['name']
#Wrong country names dataset
df=pd.read_csv("fuzzy.csv",encoding="ISO-8859-1", skipinitialspace=True)
print(df.dtypes)
wrong_names=df.dropna().values
print(wrong_names)
#Correct country names dataset
choices_df=pd.read_csv("country.csv",encoding="ISO-8859-1",sep='\t', skipinitialspace=True)
correct_names=choices_df.values
print(correct_names)
...
print(df[['correct_country_name','country_names_ratio']].head(10))
Output
Country object
alpha-2 object
alpha-3 object
country-code int64
iso_3166-2 object
region object
sub-region object
region-co int64
sub-region.1 int64
dtype: object
[[u'elbenie' u'AL' u'ALB' 8 u'ISO 3166-2:AL' u'Europe' u'Southern Europe'
150 39]
[u'enforre' u'AD' u'AND' 20 u'ISO 3166-2:AD' u'Europe' u'Southern Europe'
150 39]
[u'Belerus' u'AT' u'AUT' 40 u'ISO 3166-2:AT' u'Europe' u'Western Europe'
150 155]]
[[u'elbenie']
[u'enforre']
[u'Belerus']]
('row=', array([u'elbenie', u'AL', u'ALB', 8, u'ISO 3166-2:AL', u'Europe',
u'Southern Europe', 150, 39], dtype=object))
('row=', array([u'enforre', u'AD', u'AND', 20, u'ISO 3166-2:AD', u'Europe',
u'Southern Europe', 150, 39], dtype=object))
('row=', array([u'Belerus', u'AT', u'AUT', 40, u'ISO 3166-2:AT', u'Europe',
u'Western Europe', 150, 155], dtype=object))
correct_country_name country_names_ratio
0 [elbenie] 60
1 [enforre] 60
2 [Belerus] 60
I am trying to create an HDF5 file with two datasets, 'data' and 'label'. When I tried to access the said file, however, I got an error as follows:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevd.py", line 1664, in <module>
main()
File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevd.py", line 1658, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevd.py", line 1068, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/pycharm/Input_Pipeline.py", line 140, in <module>
data_h5 = f['data'][:]
File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "C:\Users\u20x47\PycharmProjects\PCL\venv\lib\site-packages\h5py\_hl\group.py", line 177, in __getitem__
oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py\h5o.pyx", line 190, in h5py.h5o.open
ValueError: Not a location (invalid object ID)
Code used to create the dataset:
h5_file.create_dataset('data', data=data_x, compression='gzip', compression_opts=4, dtype='float32')
h5_file.create_dataset('label', data=label, compression='gzip', compression_opts=1, dtype='uint8')
data_x an array of arrays. Each element in data_x is a 3D array of 1024 elements.
label is an array of arrays as well. Each element is a 1D array of 1 element.
Code to access the said file:
f = h5_file
data_h5 = f['data'][:]
label_h5 = f['label'][:]
print (data_h5, label_h5)
How can I fix this? Is this a syntax error or a logical one?
I was unable to reproduce the error.
Maybe you forgot to close the file or you change the content of your h5 during execution.
Also you can use print h5_file.items() to check the content of your h5 file
Tested code:
import h5py
import numpy as np
h5_file = h5py.File('test.h5', 'w')
# bogus data with the correct size
data_x = np.random.rand(16,8,8)
label = np.random.randint(100, size=(1,1),dtype='uint8')
#
h5_file.create_dataset('data', data=data_x, compression='gzip', compression_opts=4, dtype='float32')
h5_file.create_dataset('label', data=label, compression='gzip', compression_opts=1, dtype='uint8')
h5_file.close()
h5_file = h5py.File('test.h5', 'r')
f = h5_file
print f.items()
data_h5 = f['data'][...]
label_h5 = f['label'][...]
print (data_h5, label_h5)
h5_file.close()
Produces
[(u'data', <HDF5 dataset "data": shape (16, 8, 8), type "<f4">), (u'label', <HDF5 dataset "label": shape (1, 1), type "|u1">)]
(array([[[4.36837107e-01, 8.05664659e-01, 3.34415197e-01, ...,
8.89135897e-01, 1.84097692e-01, 3.60782951e-01],
[8.86442482e-01, 6.07181549e-01, 2.42844030e-01, ...,
[4.24369454e-01, 6.04596496e-01, 5.56676507e-01, ...,
7.22884715e-01, 2.45932683e-01, 9.18777227e-01]]], dtype=float32), array([[25]], dtype=uint8))
I am currently experiencing an issue whenever I try to evaluate an individual using the GP portion of DEAP.
I receive the following error:
Traceback (most recent call last):
File "ImageGP.py", line 297, in <module>
pop, logs = algorithms.eaSimple(pop, toolbox, 0.9, 0.1, 60, stats=mstats, halloffame=hof, verbose=True)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/deap/algorithms.py", line 148, in eaSimple
for ind, fit in zip(invalid_ind, fitnesses):
File "ImageGP.py", line 229, in evalFunc
func = toolbox.compile(expr=individual)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/deap/gp.py", line 451, in compile
return eval(code, pset.context, {})
File "<string>", line 1
lambda oValue,oAvg13,oAvg17,oAvg21,sobelVal(v),sobelVal(h),edgeVal,blotchVal: [[[0, 75, 82.2857142857, 83.0, 82.9090909091, 4, 12, 4, 180], ... Proceed to print out all of my data ... [0, 147, 151.244897959, 150.728395062, 150.73553719, 248, 244, 5, 210]]]
^
SyntaxError: invalid syntax
If anyone has any ideas about what could be causing this problem, then I would really appreciate some advice. My current evaluation function looks like this:
def evalFunc(individual, data, points):
func = toolbox.compile(expr=individual)
total = 1.0
for point in points:
tmp = [float(x) for x in data[point[1]][point[0]][1:9]]
total += int((0 if (func(*tmp)) < 0 else 1) == points[2])
print ("Fitness: " + str(total))
return total,
Where the data contains the data being used (the values for the 8 variables listed in the error) and point specifying the x and y co-ordinates from which to get those 8 values. Thank you for your suggestions!
This programm have to order a list of numbersbut every time i become a error can you help me to fix it? I hope somebody has an idea. I hzave also tried to us del list[(thenumber of the element or the number)]
# list = list with the none ordert number
# newlist = with the ordert numbers
# pnumbver = privious number
# add = new number for list
# numberelemente = how many numbers get in list
# length = length of list
# i = counting up for the stop
from random import randint
list = []
newlist = []
numberelemente = 10
while numberelemente > 0:
add = randint(-100, 100)
list.append(add)
numberelemente = numberelemente - 1
print(list)
pnumber=list[0]
length = len(list)
i = 0
while i < length:
for zahl in list:
if number < pnumber:
pnumber = number
list.remove(pnumber)
newlist.append(pnumber)
i = i+1
print(newlist)
but i become this error i become them every time
eenter code her>>> runfile('C:/Users/Max/Desktop/python/liste ordnen.py', wdir='C:/Users/Max/Desktop/python')
[89, 46, 68, -30, 93, 38, -73, 91, 33, -69]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Max\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "C:\Users\Max\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Max/Desktop/python/liste ordnen.py", line 29, in <module>
list.remove(vzahl)
ValueError: list.remove(x): x not in list
I never say that you added pnumber to list so it cant delete it. Don't you mean to use.
list.append(pnumber)
This is what list.remove() does
list.remove(x)
Remove the first item from the list whose value is x. It is an error if there is no such item.
-https://docs.python.org/3/tutorial/datastructures.html
Hope this helps.
I am updating a Pandas Data Frame.
The script looks up for a product. If the product is already in data frame, it just updates it columns with accumulated new values.
If the product is not there it creates a new set of rows to insert the values of the product.
Code
for m in range(0,len(product_sales_price)):
if exact_match(str(sales_record[n-1]),str(product_sales_price[m]))==True:
total_product_daily_sales = counter * product_sales_price[m+1]
'''
print(total_product_daily_sales)
'''
total_product_daily_net_profit = total_product_daily_sales *.1
print(counter)
print(product_sales_price[m+1])
print(total_product_daily_sales)
print(total_product_daily_net_profit)
print(m)
print(product_sales_price[m])
if (product_revenue_and_net_profit_df.ix[:,0] == product_sales_price[m]).any() == True :
product_revenue_and_net_profit_df.ix[:,:][(product_revenue_and_net_profit_df.ix[:,
0] == product_sales_price[m])] = [
product_revenue_and_net_profit_df.ix[:,0][(product_revenue_and_net_profit_df.ix[:,
0] == product_sales_price[m])],
product_revenue_and_net_profit_df.ix[:,1][(product_revenue_and_net_profit_df.ix[:,
0] == product_sales_price[m])]+counter,
product_revenue_and_net_profit_df.ix[:,2][(product_revenue_and_net_profit_df.ix[:,
0] == product_sales_price[
m])]+total_product_daily_sales,product_revenue_and_net_profit_df.ix[:,
3][(product_revenue_and_net_profit_df.ix[:,0] == product_sales_price[
m])]+total_product_daily_net_profit]
else:
product_revenue_and_net_profit_df.ix[(product_revenue_and_net_profit_df.shape[0]+1),:] = (
[product_sales_price[m],counter,total_product_daily_sales,
total_product_daily_net_profit]
)
Run Time
<sale_frequency time (in seconds):
1
423.44
423.44
42.344
0
Bushwacker Dodge Pocket Style Fender Flare Set of 4
Traceback (most recent call last):
File "32\scriptStarter.py", line 120, in <module>
File "C:\Python Projects\Amazon-Sales\amazon_analysis.py", line 162, in <module>
print (timeit.timeit(fn + "()", "from __main__ import "+fn, number=1))
File "C:\Users\onthego\Anaconda3\lib\timeit.py", line 219, in timeit
return Timer(stmt, setup, timer).timeit(number)
File "C:\Users\onthego\Anaconda3\lib\timeit.py", line 184, in timeit
timing = self.inner(it, self.timer)
File "<timeit-src>", line 6, in inner
File "C:\Python Projects\Amazon-Sales\amazon_analysis.py", line 91, in sale_frequency
m])]+total_product_daily_net_profit]
File "C:\Users\onthego\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2122, in __setitem__
self._setitem_array(key, value)
File "C:\Users\onthego\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2142, in _setitem_array
self.ix._setitem_with_indexer(indexer, value)
File "C:\Users\onthego\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 448, in _setitem_with_indexer
elif np.array(value).ndim == 2:
File "C:\Users\onthego\Anaconda3\lib\site-packages\pandas\core\series.py", line 521, in __getitem__
result = self.index.get_value(self, key)
File "C:\Users\onthego\Anaconda3\lib\site-packages\pandas\core\index.py", line 1595, in get_value
return self._engine.get_value(s, k)
File "pandas\index.pyx", line 100, in pandas.index.IndexEngine.get_value (pandas\index.c:3113)
File "pandas\index.pyx", line 108, in pandas.index.IndexEngine.get_value (pandas\index.c:2844)
File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:3704)
File "pandas\hashtable.pyx", line 375, in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:7224)
File "pandas\hashtable.pyx", line 381, in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:7162)
KeyError: 0
>>>
>>>
>>>