In [1]:
"""
Importare la libreria pandas e assegnarel
l'etichetta che si vuole, per esempio pd.
D'ora in poi digitando pd sulla riga di comando
richiamerà la libreria
"""
import pandas as pd
In [2]:
"""
Come esempio si può importare una tabella
contenente moltissimi dati riguardanti le vendite di 
automobili. Si assegna un indirizzo. In questo caso
si tratta di un indirizzo internet.
"""
url="https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data"
In [3]:
"""
Ora si crea il DATAFRAME. Gli si assegna un'etichetta. Una qualunque va bene
in questo caso usiamo df
Il file che si carica è una tabella scritta in formato csv e lo si carica senza
l'intestazione, cioé senza la prima riga di etichette
"""
df=pd.read_csv(url,header=None)
In [4]:
"""
Si possono stampare anche un numero limitatto di righe.
Per esempio 5
"""
df.head(15)
Out[4]:
0 1 2 3 4 5 6 7 8 9 ... 16 17 18 19 20 21 22 23 24 25
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 2 ? audi gas std two sedan fwd front 99.8 ... 136 mpfi 3.19 3.40 8.5 110 5500 19 25 15250
6 1 158 audi gas std four sedan fwd front 105.8 ... 136 mpfi 3.19 3.40 8.5 110 5500 19 25 17710
7 1 ? audi gas std four wagon fwd front 105.8 ... 136 mpfi 3.19 3.40 8.5 110 5500 19 25 18920
8 1 158 audi gas turbo four sedan fwd front 105.8 ... 131 mpfi 3.13 3.40 8.3 140 5500 17 20 23875
9 0 ? audi gas turbo two hatchback 4wd front 99.5 ... 131 mpfi 3.13 3.40 7.0 160 5500 16 22 ?
10 2 192 bmw gas std two sedan rwd front 101.2 ... 108 mpfi 3.50 2.80 8.8 101 5800 23 29 16430
11 0 192 bmw gas std four sedan rwd front 101.2 ... 108 mpfi 3.50 2.80 8.8 101 5800 23 29 16925
12 0 188 bmw gas std two sedan rwd front 101.2 ... 164 mpfi 3.31 3.19 9.0 121 4250 21 28 20970
13 0 188 bmw gas std four sedan rwd front 101.2 ... 164 mpfi 3.31 3.19 9.0 121 4250 21 28 21105
14 1 ? bmw gas std four sedan rwd front 103.5 ... 164 mpfi 3.31 3.19 9.0 121 4250 20 25 24565

15 rows × 26 columns

In [5]:
"""
È possibile inserire una stringa contenente le etichette
per ogni colonna.
"""
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"]
In [17]:
"""
Si assegna alle colonne la stringa appena costruita
e si stampano le prime 5 righe
"""
df.columns=headers
df.head(5)
Out[17]:
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 [7]:
"""
Ora si può stabilire dove salvare il file creando la path.
Occorre aggiungere il parametro r per una questione legata
al corretto utilizzo dei caratteri.
In questo caso si è scelto di salvare nel formato csv. Volendo
si può salvare anche in formato excel usando:

path=r'D:\Google Drive\scuola\PYTHON\auto.xlsx'
df.to_excel(path)

"""
path=r'D:\Google Drive\scuola\PYTHON\auto.csv'
df.to_csv(path)
In [18]:
"""
È possibile ottenere informazioni globali contenute
nel dataframe. Nell'ultima colonna ci sono le informazioni
relative al formato del dato; intero (int64), virgola mobile (float64)
stringa (object)
"""
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 205 entries, 0 to 204
Data columns (total 26 columns):
simbolo         205 non-null int64
norm-loss       205 non-null object
marca           205 non-null object
carburante      205 non-null object
asp             205 non-null object
n_di_porte      205 non-null object
stile           205 non-null object
trazione        205 non-null object
lo_motore       205 non-null object
ruote           205 non-null float64
lunghezza       205 non-null float64
larghezza       205 non-null float64
altezza         205 non-null float64
freno           205 non-null int64
tipo_motore     205 non-null object
n_cicli         205 non-null object
forma_motore    205 non-null int64
iniezione       205 non-null object
bore            205 non-null object
stroke          205 non-null object
comp-ratio      205 non-null float64
horsepower      205 non-null object
peak-rpm        205 non-null object
city-mpg        205 non-null int64
high-mpg        205 non-null int64
prezzo          205 non-null object
dtypes: float64(5), int64(5), object(16)
memory usage: 41.8+ KB
In [19]:
"""
È inoltre possibile ottenere una statistica globale
del dataframe
"""
df.describe()
Out[19]:
simbolo ruote lunghezza larghezza altezza freno forma_motore comp-ratio city-mpg high-mpg
count 205.000000 205.000000 205.000000 205.000000 205.000000 205.000000 205.000000 205.000000 205.000000 205.000000
mean 0.834146 98.756585 174.049268 65.907805 53.724878 2555.565854 126.907317 10.142537 25.219512 30.751220
std 1.245307 6.021776 12.337289 2.145204 2.443522 520.680204 41.642693 3.972040 6.542142 6.886443
min -2.000000 86.600000 141.100000 60.300000 47.800000 1488.000000 61.000000 7.000000 13.000000 16.000000
25% 0.000000 94.500000 166.300000 64.100000 52.000000 2145.000000 97.000000 8.600000 19.000000 25.000000
50% 1.000000 97.000000 173.200000 65.500000 54.100000 2414.000000 120.000000 9.000000 24.000000 30.000000
75% 2.000000 102.400000 183.100000 66.900000 55.500000 2935.000000 141.000000 9.400000 30.000000 34.000000
max 3.000000 120.900000 208.100000 72.300000 59.800000 4066.000000 326.000000 23.000000 49.000000 54.000000
In [ ]:
 
In [ ]: