Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d332ac2

Browse files
Update prosnal_database.py
1 parent 7f86352 commit d332ac2

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
Below is a complete Python solution using a class with methods to solve the problem with Pandas. This code creates the data (mimicking the SQL tables), then provides methods to run each query.
2+
3+
```python
4+
import pandas as pd
5+
6+
class ProsnalDatabase:
7+
def __init__(self):
8+
# Create tables (as Pandas DataFrames)
9+
self.create_tables()
10+
11+
def create_tables(self):
12+
# Employees Table
13+
self.employees = pd.DataFrame({
14+
'Id': [1, 2, 3, 4, 5],
15+
'Name': ['John Doe', 'Jane Smith', 'Mike Johnson', 'Sarah Black', 'David White'],
16+
'Salary': [50000, 60000, 70000, 60000, 70000],
17+
'Department': ['HR', 'Finance', 'IT', 'Finance', 'IT']
18+
})
19+
20+
# Projects Table
21+
self.projects = pd.DataFrame({
22+
'Id': [1, 2, 3],
23+
'Name': ['Project A', 'Project B', 'Project C'],
24+
'Department': ['IT', 'Finance', 'IT']
25+
})
26+
27+
# EmployeeProjects Table
28+
self.employee_projects = pd.DataFrame({
29+
'EmployeeId': [1, 1, 2, 3, 3, 4, 5],
30+
'ProjectId': [1, 2, 1, 1, 3, 2, 1]
31+
})
32+
33+
# Users Table
34+
self.users = pd.DataFrame({
35+
'id': [1, 2],
36+
'name': ['John Doe', 'Jane Smith'],
37+
'email': ['john.doe@example.com', 'antima@example.com']
38+
})
39+
40+
# Sessions Table
41+
self.sessions = pd.DataFrame({
42+
'id': [1, 2, 3],
43+
'user_id': [1, 1, 2],
44+
'session_date': pd.to_datetime(['2022年01月01日', '2022年01月02日', '2022年01月03日'])
45+
})
46+
47+
# Products Table
48+
self.products = pd.DataFrame({
49+
'id': [1, 2, 3],
50+
'name': ['Product A', 'Product B', 'Product C'],
51+
'price': [10.99, 20.99, 30.99]
52+
})
53+
54+
# Orders Table
55+
self.orders = pd.DataFrame({
56+
'id': [1, 2, 3, 4, 5],
57+
'product_id': [1, 2, 3, 1, 2],
58+
'quantity': [5, 3, 1, 2, 4]
59+
})
60+
61+
def query_employee_projects(self):
62+
"""
63+
Returns a DataFrame that lists each employee's project details.
64+
This joins the Employees, EmployeeProjects, and Projects DataFrames.
65+
"""
66+
# Merge Employees with EmployeeProjects
67+
emp_proj = self.employees.merge(
68+
self.employee_projects, left_on='Id', right_on='EmployeeId'
69+
)
70+
# Merge with Projects to get project details
71+
emp_proj = emp_proj.merge(
72+
self.projects, left_on='ProjectId', right_on='Id', suffixes=('_Employee', '_Project')
73+
)
74+
# Select and rename desired columns
75+
result = emp_proj[['Name_Employee', 'Department_Employee', 'Name_Project', 'Department_Project']]
76+
result = result.rename(columns={
77+
'Name_Employee': 'EmployeeName',
78+
'Department_Employee': 'EmployeeDepartment',
79+
'Name_Project': 'ProjectName',
80+
'Department_Project': 'ProjectDepartment'
81+
})
82+
return result
83+
84+
def query_user_sessions(self):
85+
"""
86+
Returns a DataFrame that lists the session dates for each user.
87+
This joins the Users and Sessions DataFrames.
88+
"""
89+
user_sessions = self.users.merge(
90+
self.sessions, left_on='id', right_on='user_id'
91+
)
92+
result = user_sessions[['name', 'session_date']].rename(columns={'name': 'user_name'})
93+
return result
94+
95+
def query_order_totals(self):
96+
"""
97+
Returns a DataFrame that calculates the total price for each order.
98+
This joins the Products and Orders DataFrames and computes total price = price * quantity.
99+
"""
100+
orders_merged = self.orders.merge(
101+
self.products, left_on='product_id', right_on='id'
102+
)
103+
orders_merged['total_price'] = orders_merged['price'] * orders_merged['quantity']
104+
result = orders_merged[['name', 'quantity', 'total_price']].rename(columns={'name': 'product_name'})
105+
return result
106+
107+
# Example usage:
108+
if __name__ == '__main__':
109+
db = ProsnalDatabase()
110+
111+
print("Employee Projects:")
112+
print(db.query_employee_projects(), "\n")
113+
114+
print("User Sessions:")
115+
print(db.query_user_sessions(), "\n")
116+
117+
print("Order Totals:")
118+
print(db.query_order_totals())
119+
```
120+
121+
### Explanation:
122+
- **Class Initialization (`__init__`)**:
123+
The `ProsnalDatabase` class initializes by calling `create_tables()`, which creates sample DataFrames for all tables.
124+
125+
- **`create_tables()`**:
126+
This method creates DataFrames for `Employees`, `Projects`, `EmployeeProjects`, `Users`, `Sessions`, `Products`, and `Orders` with sample data.
127+
128+
- **Query Methods**:
129+
- **`query_employee_projects()`**:
130+
Merges the `employees`, `employee_projects`, and `projects` DataFrames to show which employee works on which project. Columns are renamed for clarity.
131+
132+
- **`query_user_sessions()`**:
133+
Merges the `users` and `sessions` DataFrames to list session dates for each user.
134+
135+
- **`query_order_totals()`**:
136+
Merges the `orders` DataFrame with the `products` DataFrame and calculates the total price for each order.
137+
138+
- **Main Block**:
139+
An instance of `ProsnalDatabase` is created, and the query methods are executed to display results.
140+
141+
This modular, class-based approach using Pandas helps encapsulate data and query logic in a single, easy-to-manage unit. Let me know if you need any further modifications!

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /