In [4]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
In [5]:
"""
Ora importiamo nuovamente la tabella usata nella lezione 1
e visualizziamo le prime 5 righe
"""

url="https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data"
df=pd.read_csv(url,header=None)
headers=["simbolo","norm-loss","marca","carburante","asp","n_di_porte","stile","trazione","lo_motore","ruote","lunghezza","larghezza","altezza","freno","tipo_motore","n_cicli","forma_motore","iniezione","bore","stroke","comp-ratio","horsepower","peak-rpm","city-mpg","high-mpg","prezzo"]
df.columns=headers
df.head(5)
Out[5]:
simbolo norm-loss marca carburante asp n_di_porte stile trazione lo_motore ruote ... forma_motore iniezione bore stroke comp-ratio horsepower peak-rpm city-mpg high-mpg prezzo
0 3 ? alfa-romero gas std two convertible rwd front 88.6 ... 130 mpfi 3.47 2.68 9.0 111 5000 21 27 13495
1 3 ? alfa-romero gas std two convertible rwd front 88.6 ... 130 mpfi 3.47 2.68 9.0 111 5000 21 27 16500
2 1 ? alfa-romero gas std two hatchback rwd front 94.5 ... 152 mpfi 2.68 3.47 9.0 154 5000 19 26 16500
3 2 164 audi gas std four sedan fwd front 99.8 ... 109 mpfi 3.19 3.40 10.0 102 5500 24 30 13950
4 2 164 audi gas std four sedan 4wd front 99.4 ... 136 mpfi 3.19 3.40 8.0 115 5500 18 22 17450

5 rows × 26 columns

In [6]:
"""
gli oggetti Panda possono essere divisi su uno qualsiasi dei loro assi.
La definizione astratta di raggruppamento è fornire una mappatura delle
etichette ai nomi dei gruppi. Nel caso seguente si raggruppano due colonne e
si fanno le medie degli altri
"""
gruppo=df.groupby(["marca","prezzo"])
gruppo.mean()
Out[6]:
simbolo ruote lunghezza larghezza altezza freno forma_motore comp-ratio city-mpg high-mpg
marca prezzo
alfa-romero 13495 3.0 88.60 168.8 64.1 48.8 2548.0 130.0 9.0 21.0 27.0
16500 2.0 91.55 170.0 64.8 50.6 2685.5 141.0 9.0 20.0 26.5
audi 13950 2.0 99.80 176.6 66.2 54.3 2337.0 109.0 10.0 24.0 30.0
15250 2.0 99.80 177.3 66.3 53.1 2507.0 136.0 8.5 19.0 25.0
17450 2.0 99.40 176.6 66.4 54.3 2824.0 136.0 8.0 18.0 22.0
... ... ... ... ... ... ... ... ... ... ... ...
volvo 18950 -1.0 104.30 188.8 67.2 57.5 3157.0 130.0 7.5 17.0 22.0
19045 -1.0 109.10 188.8 68.8 55.5 3049.0 141.0 8.7 19.0 25.0
21485 -1.0 109.10 188.8 68.9 55.5 3012.0 173.0 8.8 18.0 23.0
22470 -1.0 109.10 188.8 68.9 55.5 3217.0 145.0 23.0 26.0 27.0
22625 -1.0 109.10 188.8 68.9 55.5 3062.0 141.0 9.5 19.0 25.0

199 rows × 10 columns

In [7]:
"""
Raggruppamento di tre colonne
"""
df_test=df[["ruote","stile","prezzo"]]
In [11]:
df_test
Out[11]:
ruote stile prezzo
0 88.6 convertible 13495
1 88.6 convertible 16500
2 94.5 hatchback 16500
3 99.8 sedan 13950
4 99.4 sedan 17450
... ... ... ...
200 109.1 sedan 16845
201 109.1 sedan 19045
202 109.1 sedan 21485
203 109.1 sedan 22470
204 109.1 sedan 22625

205 rows × 3 columns

In [15]:
df
Out[15]:
simbolo norm-loss marca carburante asp n_di_porte stile trazione lo_motore ruote ... forma_motore iniezione bore stroke comp-ratio horsepower peak-rpm city-mpg high-mpg prezzo
0 3 ? alfa-romero gas std two convertible rwd front 88.6 ... 130 mpfi 3.47 2.68 9.0 111 5000 21 27 13495
1 3 ? alfa-romero gas std two convertible rwd front 88.6 ... 130 mpfi 3.47 2.68 9.0 111 5000 21 27 16500
2 1 ? alfa-romero gas std two hatchback rwd front 94.5 ... 152 mpfi 2.68 3.47 9.0 154 5000 19 26 16500
3 2 164 audi gas std four sedan fwd front 99.8 ... 109 mpfi 3.19 3.40 10.0 102 5500 24 30 13950
4 2 164 audi gas std four sedan 4wd front 99.4 ... 136 mpfi 3.19 3.40 8.0 115 5500 18 22 17450
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
200 -1 95 volvo gas std four sedan rwd front 109.1 ... 141 mpfi 3.78 3.15 9.5 114 5400 23 28 16845
201 -1 95 volvo gas turbo four sedan rwd front 109.1 ... 141 mpfi 3.78 3.15 8.7 160 5300 19 25 19045
202 -1 95 volvo gas std four sedan rwd front 109.1 ... 173 mpfi 3.58 2.87 8.8 134 5500 18 23 21485
203 -1 95 volvo diesel turbo four sedan rwd front 109.1 ... 145 idi 3.01 3.40 23.0 106 4800 26 27 22470
204 -1 95 volvo gas turbo four sedan rwd front 109.1 ... 141 mpfi 3.78 3.15 9.5 114 5400 19 25 22625

205 rows × 26 columns

In [38]:
df2 = pd.DataFrame({'foo': ['one', 'one', 'one', 'two', 'two',
...                            'two'],
...                    'bar': ['A', 'B', 'C', 'A', 'B', 'C'],
...                    'baz': [1, 2, 3, 4, 5, 6],
...                    'zoo': ['x', 'y', 'z', 'q', 'w', 't']})
In [39]:
"""
Nuovo Data FRame creato
"""
df2
Out[39]:
foo bar baz zoo
0 one A 1 x
1 one B 2 y
2 one C 3 z
3 two A 4 q
4 two B 5 w
5 two C 6 t
In [40]:
"""
Tabella pivot per visualizzare
una colonna
"""

df2.pivot(index='foo', columns='bar', values='baz')
Out[40]:
bar A B C
foo
one 1 2 3
two 4 5 6
In [ ]: