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([])
Create a horizontal bar chart using column MainBranch.
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)
-
\$\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\$Sᴀᴍ Onᴇᴌᴀ– Sᴀᴍ Onᴇᴌᴀ ♦2022年08月15日 14:57:22 +00:00Commented Aug 15, 2022 at 14:57
-
\$\begingroup\$ @Reinderien sorry, added \$\endgroup\$kkkkrkrkkk– kkkkrkrkkk2022年08月15日 17:31:25 +00:00Commented Aug 15, 2022 at 17:31
1 Answer 1
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.
Explore related questions
See similar questions with these tags.