
Добавил:
KaFaka
t.me
Инфо для ГУАП студентов от меня: https://kafaka.notion.site/99e6d9b70ca74f7baef3daea17839e5a
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:4 курс 1 семестр / Лабораторные / Python / LR8_6
.py import psycopg2
import pandas as pd
import matplotlib.pyplot as plt
# Connect to the database
conn = psycopg2.connect(database="students", user="postgres", password="123", host="127.0.0.1", port="5432")
cursor = conn.cursor()
# Run the SQL query
query = "SELECT l.city, AVG(e.salary) as avg_salary " \
"FROM employees e " \
"JOIN locations l ON e.location_id = l.location_id " \
"GROUP BY l.city"
cursor.execute(query)
# Fetch the result and store it in a Pandas dataframe
result = cursor.fetchall()
df = pd.DataFrame(result, columns=["city", "avg_salary"])
# Plot the data
plt.bar(df["city"], df["avg_salary"])
plt.xlabel("Город")
plt.ylabel("Средняя зарплата")
plt.title("Средняя зарплата по городу")
plt.show()
# Close the cursor and connection
cursor.close()
conn.close()
import pandas as pd
import matplotlib.pyplot as plt
# Connect to the database
conn = psycopg2.connect(database="students", user="postgres", password="123", host="127.0.0.1", port="5432")
cursor = conn.cursor()
# Run the SQL query
query = "SELECT l.city, AVG(e.salary) as avg_salary " \
"FROM employees e " \
"JOIN locations l ON e.location_id = l.location_id " \
"GROUP BY l.city"
cursor.execute(query)
# Fetch the result and store it in a Pandas dataframe
result = cursor.fetchall()
df = pd.DataFrame(result, columns=["city", "avg_salary"])
# Plot the data
plt.bar(df["city"], df["avg_salary"])
plt.xlabel("Город")
plt.ylabel("Средняя зарплата")
plt.title("Средняя зарплата по городу")
plt.show()
# Close the cursor and connection
cursor.close()
conn.close()