Witam. Dodałem tabelę do istniejącego już pliku csv:
import pandas as pd
import plotly.express as px
from google.colab import drive
df = pd.read_csv('winequality-red.csv', delimiter=';')
conditions = [
(df['alcohol'] < (df['alcohol'].mean() - df['alcohol'].std())),
(df['alcohol'] > (df['alcohol'].mean() - df['alcohol'].std())) & (df['alcohol'] < (df['alcohol'].mean() + df['alcohol'].std())),
(df['alcohol'] > (df['alcohol'].mean() + df['alcohol'].std())),
]
# create a list of the values we want to assign for each condition
values = ['low', 'mid', 'high']
# create a new column and use np.select to assign values to it using our lists as arguments
df['alcohol_cat'] = np.select(conditions, values)
# display updated DataFrame
df.head(100)

Jednak za każdym razem gdy próbowałem utworzyć wykres z wykorzystaniem wartości z tabeli "alcohol_cat" to otrzymywałem komunikaty o błędach.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px
from google.colab import drive
df = pd.read_csv('winequality-red.csv', delimiter=';')
conditions = [
(df['alcohol'] < (df['alcohol'].mean() - df['alcohol'].std())),
(df['alcohol'] > (df['alcohol'].mean() - df['alcohol'].std())) & (df['alcohol'] < (df['alcohol'].mean() + df['alcohol'].std())),
(df['alcohol'] > (df['alcohol'].mean() + df['alcohol'].std())),
]
# create a list of the values we want to assign for each condition
values = ['low', 'mid', 'high']
# create a new column and use np.select to assign values to it using our lists as arguments
df['alcohol_cat'] = np.select(conditions, values)
df = pd.read_csv('winequality-white.csv')
subjects = ['3', '4', '5', '6', '7', '8']
dataset = df.groupby('alcohol_cat')[subjects].mean()
indx = np.arange(len(subjects))
score_label = np.arange(0, 110, 10)
L = list(dataset.T['low'])
M = list(dataset.T['mid'])
H = list(dataset.T['high'])
bar_width = 0.35
fig, ax = plt.subplots()
barLow = ax.bar(indx - bar_width/2, L, bar_width, label='L')
barMid = ax.bar(indx + bar_width/2, M, bar_width, label='M')
barHigh = ax.bar(indx + bar_width/2, H, bar_width, label='H')
# inserting x axis label
ax.set_xticks(indx)
ax.set_xticklabels(subjects)
# inserting y axis label
ax.set_yticks(score_label)
ax.set_yticklabels(score_label)
# inserting legend
ax.legend()
def insert_data_labels(bars):
for bar in bars:
bar_height = bar.get_height()
ax.annotate('{0:.0f}'.format(bar.get_height()),
xy=(bar.get_x() + bar.get_width() / 2, bar_height),
xytext=(0, 3),
textcoords='offset points',
ha='center',
va='bottom'
)
insert_data_labels(L)
insert_data_labels(M)
insert_data_labels(H)
plt.show()

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px
from google.colab import drive
df = pd.read_csv('winequality-red.csv', delimiter=';')
conditions = [
(df['alcohol'] < (df['alcohol'].mean() - df['alcohol'].std())),
(df['alcohol'] > (df['alcohol'].mean() - df['alcohol'].std())) & (df['alcohol'] < (df['alcohol'].mean() + df['alcohol'].std())),
(df['alcohol'] > (df['alcohol'].mean() + df['alcohol'].std())),
]
# create a list of the values we want to assign for each condition
values = ['low', 'mid', 'high']
df.pivot(index='quantity', columns='quality', values='alcohol_cat')
plt.show()

Moje pytanie brzmi czy jest możliwe utworzenie wykresu z danymi z dodanej tabeli czy po prostu mój kod jest nieprawidłowy? Z góry dziękuję za pomoc.