AttributeError: 'DataFrame' object has no attribute 'Vmax' - pandas

I try to solve this problem:
AttributeError: 'DataFrame' object has no attribute 'Vmax'
This is a code:
plt.figure(figsize = (15,5))
plt.plot(data.indice_tiempo, data.Vmax, label = 'Precio máximo')
plt.plot(data.indice_tiempo, data.Vmin, label = 'Precio mínimo')
plt.legend()
plt.xlabel('Fecha')
plt.ylabel('Precio')
#plt.ylim(-10,40)
plt.show()
AttributeError Traceback (most recent call last)
<ipython-input-28-e6aa3f72c6f3> in <module>
1 plt.figure(figsize = (15,5))
2
----> 3 plt.plot(data.indice_tiempo, data.Vmax, label = 'Precio máximo')
4 #plt.plot(data.indice_tiempo, data.Vmin, label = 'Precio mínimo')
5 plt.legend()
/usr/local/lib/python3.8/dist-packages/pandas/core/generic.py in __getattr__(self, name)
5485 ):
5486 return self[name]
-> 5487 return object.__getattribute__(self, name)
5488
5489 def __setattr__(self, name: str, value) -> None:
AttributeError: 'DataFrame' object has no attribute 'Vmax'

I maybe need more info to help you but I guess you are trying to plot a line representing the price of something and point the max and min price.
You can try something like this:
#Create dummy data
data_dict = {"indice_tiempo":[24,25,21,31,32,21],
"fecha":["01/06","02/06","03/06","04/06","05/06","06/06"]}
data = pd.DataFrame(data_dict)
#Set column "fecha" as index
data = data.set_index("fecha")
#Create plot
plt.figure(figsize = (15,5))
max_data = data.indice_tiempo.sort_values(ascending=False).head(1) #Filter max price data
min_data = data.indice_tiempo.sort_values(ascending=False).tail(1) #Filter min price data
plt.plot(data.index, data.indice_tiempo) #Plot line
plt.scatter(max_data.index, max_data.values, label = 'Precio máximo') #Plot max price point
plt.scatter(min_data.index, min_data.values, label = 'Precio mínimo') #Plot min price point
plt.legend()
plt.xlabel('Fecha')
plt.ylabel('Precio')
plt.show()
The error you are getting from Python is telling you that you don't have any column called "Vmax" in your dataframe.
I guess you're trying to call a function to get the max price.
For that you need to do this --> df.column_name.max() or df["column_name"].max()
Resulting PLOT

Related

Tkinter Scale to input values to pandas dataframe.head

I am trying to use Tkinter's Scale widget to pass a value to a function that should modify the value of a pandas dataframe.head(). For example, if the Tkinter scale is set to 50, the value passed to the .head method should look like this: dataframe.head(50).
My code looks like this:
def scatterplot(value):
try:
global graph
global bar_chart_df
global clean_df
global correlation
correlation = clean_df.groupby(['FACILITY ZIP', 'FACILITY NAME'])['VIOLATION DESCRIPTION'].count().reset_index(name='No. Violations').sort_values(by='No. Violations', ascending=False)
hue = correlation["FACILITY NAME"].head(value)
y = correlation["FACILITY ZIP"].head(value)
x = correlation["No. Violations"].head(value)
sns.scatterplot(x, y, hue)
plt.title('Correlation between the number of violations committed per vendor and their zip code (sample of 150)')
plt.legend(loc='upper right')
plt.figure(figsize=(30, 45), dpi=300, facecolor='w', edgecolor='k')
plt.tight_layout()
plt.show()
except:
my_error = tk.messagebox.showerror(title="Try again", message="Please load a valid dataset")
cmd_frame_3 = ttk.LabelFrame(self.window, text="Statistics", relief=tk.RIDGE)
cmd_frame_3.grid(row=2, column=2, sticky=tk.E + tk.W + tk.N + tk.S, padx=6)
my_scale = tk.Scale(entry_frame, from_=0, to=100, command=scatterplot, orient=tk.HORIZONTAL, width=8, length=200)
my_scale.grid(row=4, column=2, sticky=tk.W)
For some reason, it doesn't work and shows this error:
correlation = clean_df.groupby(['FACILITY ZIP', 'FACILITY
NAME'])['VIOLATION DESCRIPTION'].count().reset_index(name='No.
Violations').sort_values(by='No. Violations', ascending=False)
NameError: name 'clean_df' is not defined
Exception in Tkinter callback Traceback (most recent call last):
File "/opt/anaconda3/lib/python3.8/tkinter/init.py", line 1883, in
call
return self.func(*args)
What am I not seeing?

AttributeError: 'list' object has no attribute 'rgb2hex'

I am getting the error *" AttributeError: 'list' object has no attribute 'rgb2hex'" while trying to apply Kmeans clustering for my data and trying to map the clusters with folium *
map_clusters = folium.Map(location=[latitude, longitude], zoom_start=11)
# set color scheme for the clusters
x = np.arange(kclusters)
ys = [i + x + (i*x)**2 for i in range(kclusters)]
colors_array = cm.rainbow(np.linspace(0, 1, len(ys)))
rainbow = [colors.rgb2hex(i) for i in colors_array]
# add markers to the map
markers_colors = []
for lat, lon, poi, cluster in zip(banglore_merged['latitude'], banglore_merged['longitude'], banglore_merged['location'], banglore_merged['Cluster Labels']):
label = folium.Popup(str(poi) + ' Cluster ' + str(cluster), parse_html=True)
folium.CircleMarker(
[lat, lon],
radius=5,
popup=label,
color=rainbow[int(cluster)-1],
fill=True,
fill_color=rainbow[int(cluster)-1],
fill_opacity=0.7).add_to(map_clusters)
map_clusters
ERROR:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-50-8e09b66c0be6> in <module>
6 ys = [i + x + (i*x)**2 for i in range(kclusters)]
7 colors_array = cm.rainbow(np.linspace(0, 1, len(ys)))
----> 8 rainbow = [colors.rgb2hex(i) for i in colors_array]
9
10 # add markers to the map
<ipython-input-50-8e09b66c0be6> in <listcomp>(.0)
6 ys = [i + x + (i*x)**2 for i in range(kclusters)]
7 colors_array = cm.rainbow(np.linspace(0, 1, len(ys)))
----> 8 rainbow = [colors.rgb2hex(i) for i in colors_array]
9
10 # add markers to the map
AttributeError: 'list' object has no attribute 'rgb2hex'
I hope you find the answer to my query. Thanks In advance
i guess you are passing an empty dataset to the variable. kindly change the Name.
rainbow = [colors.rgb2hex(i) for i in colors_array]

How do I get the image file to be properly labeled when trying to produce topk predictions on an image?

I am trying to produce a graph of the top 5 predictions from a image classifier for any given image. My files are organized as such:
Test/ --> class_name/ --> image_number
for example,
Test/ --> chocolate/ --> 0015254.jpg
When I use the following code, I can't get the class name to redirect to print, it gives an error that results in every class being outputted (shown below).
class_to_idx:
classes = os.listdir(train_folder)
classes.sort()
#print(classes)
label_mapping = {k: v for v, k in enumerate(classes)}
class_to_idx = {classes[i]: i for i in range(len(classes))}
Here is the code to generate an image and print its top5 predictions:
def sanity_check(path):
imagepath = test_folder + path
image = process_image(imagepath)
plot = imshow(image, ax = plt)
plot.axis('off')
plot.title(class_to_idx[str(classes)])
plot.show()
axes = predict(imagepath, model)
yaxis = [class_to_idx[str(i)] for i in np.array(axes[1][0].cpu())]
y_pos = np.arange(len(yaxis))
xaxis = np.array(axes[0][0].cpu().numpy())
plt.barh(y_pos, xaxis)
plt.xlabel('probability')
plt.yticks(y_pos, yaxis)
plt.title('probability of {} classification'.format(name))
plt.show()
path = '/chocolate_mousse/1379570.jpg'
sanity_check(path)
Here is the error:
KeyError Traceback (most recent call last)
<ipython-input-64-5747e27aee19> in <module>()
1 path = '/chocolate_mousse/1379570.jpg'
----> 2 sanity_check(path, name = 'Chocolate Mousse')
<ipython-input-62-295700419574> in sanity_check(path, name)
7 plot = imshow(image, ax = plt)
8 plot.axis('off')
----> 9 plot.title(class_to_idx[str(classes)])
10 plot.show()
11
KeyError: "['apple_pie', 'baby_back_ribs', 'baklava', 'beef_carpaccio', 'beef_tartare', 'beet_salad', 'beignets', .... etc

How to get rid of "AttributeError: 'float' object has no attribute 'log2' "

Say I have a data frame with columns of min value =36884326.0, and max value =6619162563.0, which I need to plot as box plot, so I tried to log transform the values, as follows,
diff["values"] = diff['value'].apply(lambda x: (x+1))
diff["log_values"] = diff['values'].apply(lambda x: x.log2(x))
However, the above lines are throwing the error as follows,
AttributeError Traceback (most recent call last)
<ipython-input-28-fe4e1d2286b0> in <module>
1 diff['value'].max()
2 diff["values"] = diff['value'].apply(lambda x: (x+1))
----> 3 diff["log_values"] = diff['values'].apply(lambda x: x.log2(x))
~/software/anaconda/lib/python3.7/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
3192 else:
3193 values = self.astype(object).values
-> 3194 mapped = lib.map_infer(values, f, convert=convert_dtype)
3195
3196 if len(mapped) and isinstance(mapped[0], Series):
pandas/_libs/src/inference.pyx in pandas._libs.lib.map_infer()
<ipython-input-28-fe4e1d2286b0> in <lambda>(x)
1 diff['value'].max()
2 diff["values"] = diff['value'].apply(lambda x: (x+1))
----> 3 diff["log_values"] = diff['values'].apply(lambda x: x.log2(x))
AttributeError: 'float' object has no attribute 'log2'
Any suggestions would be great. Thanks
You need numpy.log2 function to aplly, please, check sintaxis here.

Setting X-limit for subplots

I am new to data visualization and have a simple question regarding x-limit range. Since I have 4 different plots, I am using 2x2 grid, using add_subplot method. Here is what I have done so far:
fig = plt.figure()
ax = fig.add_subplot(221, facecolor='lightgrey')
weeks = []
for weekNum in df.WeekNum:
weeks.append(weekNum)
maxNumber = max(weeks)
sixWeekList = list(range(maxNumber-6, maxNumber))
ax.set_title('Photo', fontsize = 14)
ax.set_xlabel('Week Number')
ax.set_xlim(sixWeekList)
ax.set_ylabel('Percentage')
ax.set_ylim([0,100])
fig.tight_layout()
My aim to bar plot the latest 6 weeks, every time I run the script. However, when I run the above program, it shows the following error, and doesn't let me set the range for x-axis. However, if I just put [30, 35] in ax.set_xlim(), it runs smoothly, ex: ax.set_xlim([30,35])
**ValueError** Traceback (most recent call last)
<ipython-input-165-cbbb1eabca92> in <module>()
---> ax.set_xlim(tmp)
ValueError: too many values to unpack (expected 2)
Any alternate way to resolve this?
Try specifying the x limits with the max and min directly rather than a list, which is what it is failing.
For example:
fig = plt.figure()
ax = fig.add_subplot(221, facecolor='lightgrey')
weeks = []
for weekNum in df.WeekNum:
weeks.append(weekNum)
maxNumber = max(weeks)
sixWeekList = list(range(maxNumber-6, maxNumber))
ax.set_title('Photo', fontsize = 14)
ax.set_xlabel('Week Number')
#just pass max and min here
ax.set_xlim(maxNumber-6, maxNumber)
ax.set_ylabel('Percentage')
ax.set_ylim([0,100])