Aplicație în Python care gestionează datele despre elevi, note, materii și medii, și afișează un grafic cu aceste informații.
Vom folosi biblioteci precum matplotlib pentru grafice și pandas pentru manipularea datelor.
folder scoala cu situatie_scolara.py:
import pandas as pd
import matplotlib.pyplot as plt
import random
# Generăm 20 de elevi cu note random
elevi = []
materii = ["Matematica", "Romana", "Informatica"]
for i in range(20):
nume = f"Elev {i+1}"
note = {materie: random.randint(5, 10) for materie in materii}
elevi.append({"nume": nume, "materii": note})
# Crearea unui DataFrame cu datele
elevi_data = []
for elev in elevi:
nume = elev["nume"]
materii = elev["materii"]
medii = sum(materii.values()) / len(materii) # calculul mediei
elevi_data.append({"Nume": nume, **materii, "Media": medii})
# Creăm un DataFrame Pandas
df = pd.DataFrame(elevi_data)
# Afișăm tabelul cu datele elevilor
print(df)
# Graficul mediilor pe elevi
plt.figure(figsize=(10, 6))
plt.bar(df["Nume"], df["Media"], color='skyblue')
plt.xlabel("Elevi")
plt.ylabel("Media")
plt.title("Mediile elevilor")
plt.xticks(rotation=45)
plt.tight_layout()
# Afișăm graficul
plt.show()
# Graficul notelor pe materii pentru fiecare elev
materii = df.columns[1:-1] # excluzând "Nume" și "Media"
for materie in materii:
plt.figure(figsize=(10, 6))
plt.bar(df["Nume"], df[materie], color='orange')
plt.xlabel("Elevi")
plt.ylabel(f"Nota la {materie}")
plt.title(f"Notele elevilor la {materie}")
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
PS C:\Users\Florin\Desktop\scoala> & C:/msys64/ucrt64/bin/python.exe "c:/Users/Florin/scoala/situatie scolara.py"
Traceback (most recent call last):
File "c:/Users/Florin/scoala/situatie scolara.py", line 1, in <module>
import pandas as pd
ModuleNotFoundError: No module named 'pandas'
PS C:\Users\Florin\Desktop\scoala> pip install pandas
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pandas in c:\users\florin\appdata\roaming\python\python311\site-packages (2.2.2)
Requirement already satisfied: numpy>=1.23.2 in c:\users\florin\appdata\roaming\python\python311\site-packages (from pandas) (2.0.0)
Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\florin\appdata\roaming\python\python311\site-packages (from pandas) (2.9.0.post0)
Requirement already satisfied: pytz>=2020.1 in c:\users\florin\appdata\roaming\python\python311\site-packages (from pandas) (2024.1)
Requirement already satisfied: tzdata>=2022.7 in c:\users\florin\appdata\roaming\python\python311\site-packages (from pandas) (2024.1)
Requirement already satisfied: six>=1.5 in c:\users\florin\appdata\roaming\python\python311\site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)
[notice] A new release of pip is available: 24.2 -> 24.3.1
[notice] To update, run: python.exe -m pip install --upgrade pip
PS C:\Users\Florin\Desktop\scoala> pip3 install pandas
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pandas in c:\users\florin\appdata\roaming\python\python311\site-packages (2.2.2)
Requirement already satisfied: numpy>=1.23.2 in c:\users\florin\appdata\roaming\python\python311\site-packages (from pandas) (2.0.0)
Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\florin\appdata\roaming\python\python311\site-packages (from pandas) (2.9.0.post0)
Requirement already satisfied: pytz>=2020.1 in c:\users\florin\appdata\roaming\python\python311\site-packages (from pandas) (2024.1)
Requirement already satisfied: tzdata>=2022.7 in c:\users\florin\appdata\roaming\python\python311\site-packages (from pandas) (2024.1)
Requirement already satisfied: six>=1.5 in c:\users\florin\appdata\roaming\python\python311\site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)
[notice] A new release of pip is available: 24.2 -> 24.3.1
[notice] To update, run: python.exe -m pip install --upgrade pip
PS C:\Users\Florin\Desktop\scoala> python -m pip show pandas
Name: pandas
Version: 2.2.2
Summary: Powerful data structures for data analysis, time series, and statistics
Home-page: https://pandas.pydata.org
Author:
Author-email: The Pandas Development Team <pandas-dev@python.org>
License: BSD 3-Clause License
Copyright (c) 2008-2011, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
All rights reserved.
Copyright (c) 2011-2023, Open source contributors.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Location: C:\Users\Florin\AppData\Roaming\Python\Python311\site-packages
Requires: numpy, python-dateutil, pytz, tzdata
Required-by: geopandas, seaborn
PS C:\Users\Florin\Desktop\scoala> python -m pip show pandas
Name: pandas
Version: 2.2.2
Summary: Powerful data structures for data analysis, time series, and statistics
Home-page: https://pandas.pydata.org
Author:
Author-email: The Pandas Development Team <pandas-dev@python.org>
License: BSD 3-Clause License
Copyright (c) 2008-2011, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
All rights reserved.
Copyright (c) 2011-2023, Open source contributors.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Location: C:\Users\Florin\AppData\Roaming\Python\Python311\site-packages
Requires: numpy, python-dateutil, pytz, tzdata
Required-by: geopandas, seaborn
PS C:\Users\Florin\Desktop\scoala> python "c:/Users/Florin/scoala/situatie scolara.py"
Nume Matematica Romana Informatica Media
0 Elev 1 5 5 10 6.666667
1 Elev 2 7 6 9 7.333333
2 Elev 3 7 10 10 9.000000
3 Elev 4 6 6 7 6.333333
4 Elev 5 10 10 8 9.333333
5 Elev 6 5 10 5 6.666667
6 Elev 7 9 5 6 6.666667
7 Elev 8 10 8 10 9.333333
8 Elev 9 7 5 9 7.000000
9 Elev 10 9 9 5 7.666667
10 Elev 11 9 6 10 8.333333
11 Elev 12 6 10 5 7.000000
12 Elev 13 7 9 7 7.666667
13 Elev 14 10 7 5 7.333333
14 Elev 15 6 8 5 6.333333
15 Elev 16 9 5 7 7.000000
16 Elev 17 6 7 9 7.333333
17 Elev 18 7 7 7 7.000000
18 Elev 19 8 10 10 9.333333
19 Elev 20 9 10 8 9.000000
PS C:\Users\Florin\Desktop\scoala>
Comentarii
Trimiteți un comentariu