0% found this document useful (0 votes)
6 views13 pages

Python Data Analysis with Pandas

The document contains a series of Python code cells that perform basic arithmetic operations, data comparisons, and data manipulation using the Pandas library. It demonstrates loading data from a Google Sheets CSV export into a DataFrame, displaying the data, and performing various operations such as sorting and transposing the DataFrame. The outputs of the code cells include results of calculations, data previews, and data types.

Uploaded by

enamichak00
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

Python Data Analysis with Pandas

The document contains a series of Python code cells that perform basic arithmetic operations, data comparisons, and data manipulation using the Pandas library. It demonstrates loading data from a Google Sheets CSV export into a DataFrame, displaying the data, and performing various operations such as sorting and transposing the DataFrame. The outputs of the code cells include results of calculations, data previews, and data types.

Uploaded by

enamichak00
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Text cell <rNX97RfvKyEP>

# %% [markdown]
Python

Code cell <lHpaDjBUAQsD>


# %% [code]
#question 1
a = 1
b = 2
somme = a+b
puissance = a**b
print("Somme:", somme)
print("Puissance:", puissance)
Execution output from Feb 15, 2025 10:56 AM

Stream
Somme: 3
Puissance: 1

Code cell <lLBz54V_HTyQ>


# %% [code]
#question 2
print('a==b',a==b)
print('a!=b',a!=b)
print('a>b',a>b)
print('a>=b',a>=b)
print('a<b',a<b)
print('a<=b',a<=b)
Execution output from Feb 15, 2025 11:27 AM

Stream
a==b False
a!=b True
a>b False
a>=b False
a<b True
a<=b True

Code cell <-EFqK5rbH-Uh>


# %% [code]
#question 3
type_a = type(a)
type_b = type(b)
print("Type de a:", type_a, "Type de b:", type_b)
Execution output from Feb 15, 2025 11:22 AM

Stream
Type de a: <class 'int'> Type de b: <class 'int'>

# %% [code]
#question 4
def srotListe(liste):
return sorted(liste)
liste_inw=[1,2,3,4,5,6,7,8,9]
print("liste triée:",srotListe(liste_inw))
Execution output from Feb 15, 2025 11:31 AM
Stream
liste triée: [1, 2, 3, 4, 5, 6, 7, 8, 9]

# %% [markdown]
Data manipulation

Code cell <PnF88U1fPCgp>


# %% [code]
#question 1
import pandas as pd

# ID du fichier Google Sheets (remplacez avec le vôtre)


sheet_id = "1n5u3PUI7KCgNx7g9vyWvJXiaolPVbW5m"

# URL d'export en CSV


sheet_url = f"[Link]
tqx=out:csv"

# Charger les données dans un DataFrame Pandas


df = pd.read_csv(sheet_url)

# Afficher les premières lignes


print([Link]())
Execution output from Feb 15, 2025 12:37 PM

Stream
fixed acidity volatile acidity citric acid residual sugar chlorides
\
0 11.6 0.580 0.66 2.2
0.074
1 10.4 0.610 0.49 2.1
0.200
2 7.4 1.185 0.00 NaN
0.097
3 10.4 0.440 0.42 1.5
0.145
4 8.3 1.020 0.02 3.4
0.084

free sulfur dioxide total sulfur dioxide density pH sulphates


\
0 10.0 47.0 1.20080 3.25 0.57
1 5.0 16.0 0.99940 3.16 0.63
2 5.0 14.0 0.99660 3.63 0.54
3 34.0 48.0 0.99832 3.38 0.86
4 6.0 11.0 0.99892 3.48 0.49

alcohol quality
0 NaN 3
1 8.4 3
2 10.7 3
3 9.9 3
4 NaN 3

Code cell <0nQJ4qMfP5I2>


# %% [code]
#question 2
print([Link]())
print([Link]())
print([Link](5))
Execution output from Feb 15, 2025 11:57 AM
3KB
Stream
fixed acidity volatile acidity citric acid residual sugar chlorides \
0 11,6 0,58 0,66 2,2 0,074
1 10,4 0,61 0,49 2,1 0,2
2 7,4 1,185 0 4,25 0,097
3 10,4 0,44 0,42 1,5 0,145
4 8,3 1,02 0,02 3,4 0,084

free sulfur dioxide total sulfur dioxide density pH sulphates


alcohol \
0 10 47 1,0008 3,25 0,57
9
1 5 16 0,9994 3,16 0,63
8,4
2 5 14 0,9966 3,63 0,54
10,7
3 34 48 0,99832 3,38 0,86
9,9
4 6 11 0,99892 3,48 0,49
11

quality
0 3
1 3
2 3
3 3
4 3
fixed acidity volatile acidity citric acid residual sugar
chlorides \
1594 8,6 0,42 0,39 1,8
0,068
1595 5,5 0,49 0,03 1,8
0,044
1596 7,2 0,33 0,33 1,7
0,061
1597 7,2 0,38 0,31 2
0,056
1598 7,4 0,36 0,3 1,8
0,074

free sulfur dioxide total sulfur dioxide density pH sulphates


\
1594 6 12 0,99516 3,35 0,69
1595 28 87 0,9908 3,5 0,82
1596 3 13 0,996 3,23 1,1
1597 15 29 0,99472 3,23 0,76
1598 17 24 0,99419 3,24 0,7

alcohol quality
1594 11,7 8
1595 14 8
1596 10 8
1597 11,3 8
1598 11,4 8
fixed acidity volatile acidity citric acid residual sugar
chlorides \
1506 6,8 0,36 0,32 1,8
0,067
906 11,9 0,39 0,69 2,8
0,095
1455 8,2 0,28 0,4 2,4
0,052
745 7,9 0,32 0,51 1,8
0,341
585 7,5 0,61 0,2 1,7
0,076

free sulfur dioxide total sulfur dioxide density pH sulphates


\
1506 4 8 0,9928 3,36 0,55
906 17 35 0,9994 3,1 0,61
1455 4 10 0,99356 3,33 0,7
745 17 56 0,9969 3,04 1,08
585 36 60 0,99494 3,1 0,4

alcohol quality
1506 12,8 7
906 10,8 6
1455 12,8 7
745 9,2 6
585 9,3 5

Code cell <MMnGHDlfQes3>


# %% [code]
#question 3
print([Link])
Execution output from Feb 15, 2025 11:59 AM
0KB
Stream
Index(['fixed acidity', 'volatile acidity', 'citric acid', 'residual
sugar',
'chlorides', 'free sulfur dioxide', 'total sulfur dioxide',
'density',
'pH', 'sulphates', 'alcohol', 'quality'],
dtype='object')

Code cell <v4oOf3jNRr33>


# %% [code]
#question 4
df.T
Execution output from Feb 15, 2025 12:04 PM
18KB
text/plain
0 1 2 3 4 5 \
fixed acidity 11,6 10,4 7,4 10,4 8,3 7,6
volatile acidity 0,58 0,61 1,185 0,44 1,02 1,58
citric acid 0,66 0,49 0 0,42 0,02 0
residual sugar 2,2 2,1 4,25 1,5 3,4 2,1
chlorides 0,074 0,2 0,097 0,145 0,084 0,137
free sulfur dioxide 10 5 5 34 6 5
total sulfur dioxide 47 16 14 48 11 9
density 1,0008 0,9994 0,9966 0,99832 0,99892 0,99476
pH 3,25 3,16 3,63 3,38 3,48 3,5
sulphates 0,57 0,63 0,54 0,86 0,49 0,4
alcohol 9 8,4 10,7 9,9 11 10,9
quality 3 3 3 3 3 3

6 7 8 9 ... 1589
1590 \
fixed acidity 6,8 7,3 7,1 6,7 ... 5
7,8
volatile acidity 0,815 0,98 0,875 0,76 ... 0,42
0,57
citric acid 0 0,05 0,05 0,02 ... 0,24
0,09
residual sugar 1,2 2,1 5,7 1,8 ... 2
2,3
chlorides 0,267 0,061 0,082 0,078 ... 0,06
0,065
free sulfur dioxide 16 20 3 6 ... 19
34
total sulfur dioxide 29 49 14 12 ... 50
45
density 0,99471 0,99705 0,99808 0,996 ... 0,9917
0,99417
pH 3,32 3,31 3,4 3,55 ... 3,72
3,46
sulphates 0,51 0,55 0,52 0,63 ... 0,74
0,74
alcohol 9,8 9,7 10,2 9,95 ... 14
12,7
quality 3 3 3 3 ... 8
8

1591 1592 1593 1594 1595 1596


\
fixed acidity 9,1 10 7,9 8,6 5,5 7,2
volatile acidity 0,4 0,26 0,54 0,42 0,49 0,33
citric acid 0,5 0,54 0,34 0,39 0,03 0,33
residual sugar 1,8 1,9 2,5 1,8 1,8 1,7
chlorides 0,071 0,083 0,076 0,068 0,044 0,061
free sulfur dioxide 7 42 8 6 28 3
total sulfur dioxide 16 74 17 12 87 13
density 0,99462 0,99451 0,99235 0,99516 0,9908 0,996
pH 3,21 2,98 3,2 3,35 3,5 3,23
sulphates 0,69 0,63 0,72 0,69 0,82 1,1
alcohol 12,5 11,8 13,1 11,7 14 10
quality 8 8 8 8 8 8

1597 1598
fixed acidity 7,2 7,4
volatile acidity 0,38 0,36
citric acid 0,31 0,3
residual sugar 2 1,8
chlorides 0,056 0,074
free sulfur dioxide 15 17
total sulfur dioxide 29 24
density 0,99472 0,99419
pH 3,23 3,24
sulphates 0,76 0,7
alcohol 11,3 11,4
quality 8 8

[12 rows x 1599 columns]

Code cell <mz3rZLGwSDNn>


# %% [code]
#question 5
df.sort_values(by='density')
Execution output from Feb 15, 2025 12:08 PM
18KB
text/plain
fixed acidity volatile acidity citric acid residual sugar chlorides \
1112 8 0,18 0,37 0,9
0,049
1113 8 0,18 0,37 0,9
0,049
1159 5 0,4 0,5 4,3
0,046
1461 6,7 0,28 0,28 2,4
0,012
1460 6,7 0,28 0,28 2,4
0,012
... ... ... ... ... ..
.
322 15,6 0,645 0,49 4,2
0,095
320 15,5 0,645 0,49 4,2
0,095
1426 15,6 0,685 0,76 3,7
0,1
1311 10,2 0,54 0,37 15,4
0,214
1312 10,2 0,54 0,37 15,4
0,214

free sulfur dioxide total sulfur dioxide density pH sulphates


\
1112 36 109 0,99007 2,89 0,44
1113 36 109 0,99007 2,89 0,44
1159 29 80 0,9902 3,49 0,66
1461 36 100 0,99064 3,26 0,39
1460 36 100 0,99064 3,26 0,39
... ... ... ... ... ...
322 10 23 1,00315 2,92 0,74
320 10 23 1,00315 2,92 0,74
1426 6 43 1,0032 2,95 0,68
1311 55 95 1,00369 3,18 0,77
1312 55 95 1,00369 3,18 0,77

alcohol quality
1112 12,7 6
1113 12,7 6
1159 13,6 6
1461 11,7 7
1460 11,7 7
... ... ...
322 11,1 5
320 11,1 5
1426 11,2 7
1311 9 6
1312 9 6

[1599 rows x 12 columns]

Code cell <4orvx-3xS8Jw>


# %% [code]
#question 6
print(df['pH'][1],df['pH'][2])
Execution output from Feb 15, 2025 12:13 PM
0KB
Stream
3,16 3,63

Code cell <dX22KZwuUDJm>


# %% [code]
#question 7
print([Link][5:8,['density','quality']])
Execution output from Feb 15, 2025 12:20 PM
0KB
Stream
density quality
5 0,99476 3
6 0,99471 3
7 0,99705 3
8 0,99808 3

Code cell <28DcEFxsVzqn>


# %% [code]
#question 8
df[df['quality']==4]
Execution output from Feb 15, 2025 12:22 PM
38KB
text/plain
fixed acidity volatile acidity citric acid residual sugar chlorides \
10 7,4 0,59 0,08 4,4 0,086
11 5,7 1,13 0,09 1,5 0,172
12 8,8 0,61 0,3 2,8 0,088
13 4,6 0,52 0,15 2,1 0,054
14 8,3 0,675 0,26 2,1 0,084
15 8,3 0,625 0,2 1,5 0,08
16 5 1,02 0,04 1,4 0,045
17 9,2 0,52 1 3,4 0,61
18 7,6 0,68 0,02 1,3 0,072
19 7,3 0,55 0,03 1,6 0,072
20 7,9 0,885 0,03 1,8 0,058
21 6,9 1,09 0,06 2,1 0,061
22 8,4 0,635 0,36 2 0,089
23 7 0,975 0,04 2 0,087
24 8,1 0,87 0 3,3 0,096
25 12,5 0,46 0,49 4,5 0,07
26 10,5 0,59 0,49 2,1 0,07
27 9,9 0,5 0,24 2,3 0,103
28 8,2 0,915 0,27 2,1 0,088
29 10,1 0,935 0,22 3,4 0,105
30 8,3 0,845 0,01 2,2 0,07
31 7,1 0,84 0,02 4,4 0,096
32 7,5 0,38 0,48 2,6 0,073
33 9,1 0,765 0,04 1,6 0,078
34 7,5 1,115 0,1 3,1 0,086
35 6,9 0,39 0,24 2,1 0,102
36 7,5 0,685 0,07 2,5 0,058
37 11,6 0,47 0,44 1,6 0,147
38 7,3 0,35 0,24 2 0,067
39 7,1 0,47 0 2,2 0,067
40 8,4 0,67 0,19 2,2 0,093
41 12 0,63 0,5 1,4 0,071
42 6,5 0,58 0 2,2 0,096
43 6,5 0,88 0,03 5,6 0,079
44 8,8 0,955 0,05 1,8 0,075
45 10,2 0,23 0,37 2,2 0,057
46 6 0,33 0,32 12,9 0,054
47 8,1 0,73 0 2,5 0,081
48 6,5 0,67 0 4,3 0,057
49 6,3 1,02 0 2 0,083
50 8,2 0,78 0 2,2 0,089
51 8,5 0,4 0,4 6,3 0,05
52 7,5 0,755 0 1,9 0,084
53 6,8 0,68 0,09 3,9 0,068
54 8 0,83 0,27 2 0,08
55 6,6 0,61 0 1,6 0,069
56 6,4 0,53 0,09 3,9 0,123
57 6,2 0,785 0 2,1 0,06
58 6,7 1,04 0,08 2,3 0,067
59 5,6 0,62 0,03 1,5 0,08
60 7,2 0,58 0,54 2,1 0,114
61 6,8 0,91 0,06 2 0,06
62 6,9 0,48 0,2 1,9 0,082

free sulfur dioxide total sulfur dioxide density pH sulphates


alcohol \
10 6 29 0,9974 3,38 0,5
9
11 7 19 0,994 3,5 0,48
9,8
12 17 46 0,9976 3,26 0,51
9,3
13 8 65 0,9934 3,9 0,56
13,1
14 11 43 0,9976 3,31 0,53
9,2
15 27 119 0,9972 3,16 1,12
9,1
16 41 85 0,9938 3,75 0,48
10,5
17 32 69 0,9996 2,74 2
9,4
18 9 20 0,9965 3,17 1,08
9,2
19 17 42 0,9956 3,37 0,48
9
20 4 8 0,9972 3,36 0,33
9,1
21 12 31 0,9948 3,51 0,43
11,4
22 15 55 0,99745 3,31 0,57
10,4
23 12 67 0,99565 3,35 0,6
9,4
24 26 61 1,00025 3,6 0,72
9,8
25 26 49 0,9981 3,05 0,57
9,6
26 14 47 0,9991 3,3 0,56
9,6
27 6 14 0,9978 3,34 0,52
10
28 7 23 0,9962 3,26 0,47
10
29 11 86 1,001 3,43 0,64
11,3
30 5 14 0,9967 3,32 0,58
11
31 5 13 0,997 3,41 0,57
11
32 22 84 0,9972 3,32 0,7
9,6
33 4 14 0,998 3,29 0,54
9,7
34 5 12 0,9958 3,54 0,6
11,2
35 4 7 0,99462 3,44 0,58
11,4
36 5 9 0,99632 3,38 0,55
10,9
37 36 51 0,99836 3,38 0,86
9,9
38 28 48 0,99576 3,43 0,54
10
39 7 14 0,99517 3,4 0,58
10,9
40 11 75 0,99736 3,2 0,59
9,2
41 6 26 0,99791 3,07 0,6
10,4
42 3 13 0,99557 3,62 0,62
11,5
43 23 47 0,99572 3,58 0,5
11,2
44 5 19 0,99616 3,3 0,44
9,6
45 14 36 0,99614 3,23 0,49
9,3
46 6 113 0,99572 3,3 0,56
11,5
47 12 24 0,99798 3,38 0,46
9,6
48 11 20 0,99488 3,45 0,56
11,8
49 17 24 0,99437 3,59 0,55
11,2
50 13 26 0,9978 3,37 0,46
9,6
51 3 10 0,99566 3,28 0,56
12
52 6 12 0,99672 3,34 0,49
9,7
53 15 29 0,99524 3,41 0,52
11,1
54 11 63 0,99652 3,29 0,48
9,8
55 4 8 0,99396 3,33 0,37
10,4
56 14 31 0,9968 3,5 0,67
11
57 6 13 0,99664 3,59 0,61
10
58 19 32 0,99648 3,52 0,57
11
59 6 13 0,99498 3,66 0,62
10,1
60 3 9 0,99719 3,33 0,57
10,3
61 4 11 0,99592 3,53 0,64
10,9
62 9 23 0,99585 3,39 0,43
9,05

quality
10 4
11 4
12 4
13 4
14 4
15 4
16 4
17 4
18 4
19 4
20 4
21 4
22 4
23 4
24 4
25 4
26 4
27 4
28 4
29 4
30 4
31 4
32 4
33 4
34 4
35 4
36 4
37 4
38 4
39 4
40 4
41 4
42 4
43 4
44 4
45 4
46 4
47 4
48 4
49 4
50 4
51 4
52 4
53 4
54 4
55 4
56 4
57 4
58 4
59 4
60 4
61 4
62 4

Code cell <TBJJU3nmXIP2>


# %% [code]
#question 9
df[(df['density'] < 1 )&(df['pH'] > 3.2 )]
Execution output from Feb 15, 2025 12:37 PM
19KB
text/plain
fixed acidity volatile acidity citric acid residual sugar chlorides
\
2 7.4 1.185 0.00 NaN
0.097
3 10.4 0.440 0.42 1.5
0.145
4 8.3 1.020 0.02 3.4
0.084
5 7.6 1.580 0.00 2.1
0.137
6 6.8 0.815 0.00 1.2
0.267
... ... ... ... ...
...
1594 8.6 0.420 0.39 1.8
0.068
1595 5.5 0.490 0.03 1.8
0.044
1596 7.2 0.330 0.33 1.7
0.061
1597 7.2 0.380 0.31 NaN
0.056
1598 7.4 0.360 0.30 1.8
0.074

free sulfur dioxide total sulfur dioxide density pH


sulphates \
2 5.0 14.0 0.99660 3.63
0.54
3 34.0 48.0 0.99832 3.38
0.86
4 6.0 11.0 0.99892 3.48
0.49
5 5.0 9.0 0.99476 3.50
0.40
6 16.0 29.0 0.99471 3.32
0.51
... ... ... ... ... .
..
1594 6.0 12.0 0.99516 3.35
0.69
1595 28.0 87.0 0.99080 3.50
0.82
1596 3.0 13.0 0.99600 3.23
1.10
1597 15.0 29.0 0.99472 3.23
0.76
1598 17.0 24.0 0.99419 3.24
0.70

alcohol quality
2 10.7 3
3 9.9 3
4 NaN 3
5 10.9 3
6 9.8 3
... ... ...
1594 11.7 8
1595 NaN 8
1596 NaN 8
1597 11.3 8
1598 11.4 8

[1184 rows x 12 columns]

Code cell <hF0Qvn-SaBc4>


# %% [code]
#question 10
[Link]('quality').size()
Execution output from Feb 15, 2025 12:44 PM
1KB
text/plain
quality
3 10
4 53
5 681
6 638
7 199
8 18
dtype: int64

# Remplacement des valeurs manquantes par la moyenne


[Link]([Link](), inplace=True)

# Supprimer les doublons


df.drop_duplicates(inplace=True)

# Normalisation des colonnes


df_normalized = (df - [Link]()) / ([Link]() - [Link]())

# Séparer X et Y
X = [Link](columns=['qualité'])
Y = df['qualité']

# Division en train/test
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2,
random_state=42

You might also like