Des soucis avec la création de liste de listes et l’utilisation de .unique()

(In English below)

Bonjour,

Voici l’exercice: Il s’agit d’utiliser des boîtes à moustache ou la méthode boxplot() de matplotlib.pyplot
Si j’ai bien compris, dans cet exercice, on va créer une liste de listes et du coup, on va afficher les listes
sous forme de graphique à moustache.

Pour moi, la difficulté est de comprendre la création de la liste des listes. J’ai essayé d’en créer une avec
des nombres aléatoires mais sans succès car les commandes de l’exercice ne fonctionnent pas.

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

df['Mois']= df.Month.apply(lambda x : x[3:])
print(df.head(3))
l=list()
print("shape = ", df.Mois.shape)
for i in df.Mois.unique():
    l.append(df[df['Mois'] == i]['Turnover'])
print(l[0:3])
plt.boxplot(l);
plt.xticks(range(1,13),df.Mois.unique());

plt.show();

ça produit:

     Month  Product1  Product2  Returns  Turnover  Mois
 0  01-janv       266       355        0     25285  janv
 1  01-fevr       145       204        6     14255  fevr
 2  01-mars       183       196       11     15225  mars
 shape =  (36,)
 [0     25285
 12    15700
 24    17490
 Name: Turnover, dtype: int64, 1     14255
 13    19660
 25    29665
 Name: Turnover, dtype: int64, 2     15225
14    15360
 26    22815
 Name: Turnover, dtype: int64]
je ne comprends par quel mécanisme la boucle crée une succession de tables.

J’ai essayé de recréer un exemple pour faire la même chose avec des nombres.
k = list()
nbre = np.random.choice(11,40)
NBR = pd.DataFrame(nbre)
print("shape =",NBR.shape)
for n in NBR.unique():
    k.append(n)
print(k)

ValueError: Buffer has wrong number of dimensions (expected 1, got 2)

Pourquoi ce qui suit fonctionne ?

k = list()
nbre = np.random.choice(11,40)
for n in pd.unique(nbre):
    k.append(n)
print(k)

Par contre,

k = list()
nbr = np.random.choice(11,40)
nbre = pd.DataFrame(nbr)
#print(nbre)
for n in nbre.unique():
    k.append(n)
print(k)

Ne fonctionne pas…
Je pensais créer une data frame à partir de laquelle je pourrai créer une liste de listes à partir de
nombres aléatoires et de .unique() mais ça rate…

A+
Michel

In English:

Here is the exercise: We have to use the boxplot() method of matplotlib.pyplot
If I understood well, in this exercise, we will create a list of lists and then, we will display the lists
as a moustache graph.

For me, the difficulty is to understand the creation of the list of lists. I tried to create one with
random numbers but without success because the commands of the exercise do not work.

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

df['Month']= df.Month.apply(lambda x : x[3:])
print(df.head(3))
l=list()
print("shape = ", df.Month.shape)
for i in df.Month.unique():
    l.append(df[df['Month'] == i]['Turnover'])
print(l[0:3])
plt.boxplot(l);
plt.xticks(range(1,13),df.Month.unique());

plt.show();

That produces:


     Month Product1 Product2 Returns Turnover Month
 0 01-Jan 266 355 0 25285 Jan
 1 01-Feb 145 204 6 14255 Feb
 2 01-March 183 196 11 15225 March
 shape = (36,)
 [0 25285
 12 15700
 24 17490
 Name: Turnover, dtype: int64, 1 14255
 13 19660
 25 29665
 Name: Turnover, dtype: int64, 2 15225
14 15360
 26 22815
 Name: Turnover, dtype: int64]
I don't understand by what mechanism the loop creates a succession of tables.

I tried to recreate an example to do the same thing with numbers.
k = list()
nbre = np.random.choice(11,40)
NBR = pd.DataFrame(nbre)
print("shape =",NBR.shape)
for n in NBR.unique():
    k.append(n)
print(k)

ValueError: Buffer has wrong number of dimensions (expected 1, got 2)
Why does the following work?
k = list()
nbre = np.random.choice(11,40)
for n in pd.unique(nbre):
    k.append(n)
print(k)

On the other hand,

k = list()
nbr = np.random.choice(11,40)
nbr = pd.DataFrame(nbr)
#print(nbr)
for n in nbr.unique():
    k.append(n)
print(k)
Does not work...
I was thinking of creating a data frame from which I could create a list of lists from
random numbers and .unique() but it fails...

Regards,
Atapalou

Hi @Atapalou

we here are in the forum focusing on KNIME Use cases, is this something you do want to reimplement with KNIME maybe?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.