1
\$\begingroup\$

I'm at the end of the IBM Data Analyst course, and I wanted to ask for a rating of a piece of code I wrote as a solution to its exercises from the final chapter. I know I could write it on the forum of the course, but I will finish it very soon and have no access to the forum before anyone will answer.

Exercises:

Create a stacked chart of median WorkWeekHrs and CodeRevHrs for the age group 30 to 35. data

query = "SELECT WorkWeekHrs, CodeRevHrs FROM master WHERE Age BETWEEN 30 AND 35;"
dejtafrejm = pd.read_sql_query(query, conn)
newframe = pd.DataFrame()
newframe['WorkWeekHrs'] = dejtafrejm[['WorkWeekHrs']].median()
newframe['CodeRevHrs'] = dejtafrejm['CodeRevHrs'].median()
ax = newframe.plot(kind='bar', color=['Blue', 'Yellow'], stacked=True, figsize=(5, 4))
for container in ax.containers:
 ax.bar_label(container, label_type='center')
plt.xticks([])

result

Create a horizontal bar chart using column MainBranch.

data

query = 'SELECT MainBranch FROM master'
df = pd.read_sql_query(query, conn)
df[['Total']] = 1
newdf = dief.groupby('MainBranch', axis=0).sum().transpose()
ax = newdf.plot(kind='barh', figsize=(15, 6))
for container in ax.containers:
 ax.bar_label(container)

result

Reinderien
70.9k5 gold badges76 silver badges256 bronze badges
asked Aug 15, 2022 at 13:45
\$\endgroup\$
2
  • \$\begingroup\$ Welcome to Code Review! I changed the title so that it describes what the code does per site goals: "State what your code does in your title, not your main concerns about it.". Feel free to edit and give it a different title if there is something more appropriate. \$\endgroup\$ Commented Aug 15, 2022 at 14:57
  • \$\begingroup\$ @Reinderien sorry, added \$\endgroup\$ Commented Aug 15, 2022 at 17:31

1 Answer 1

1
\$\begingroup\$

You haven't indicated what kind of database you're connected to, but: the most important change here is for you to move your aggregation from Pandas into your SQL query. This applies to your median and group sum operations. This is needed for the queries to be scalable, otherwise you're carrying a potentially huge amount of data into application memory that you don't actually need.

answered Aug 16, 2022 at 1:09
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.