Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def q5(df):
- print("Question 5:")
- psframe = pd.pivot_table(df, index=["Item"], values =["Category","Protein", "Sugars"]) # create a new df with only item, proteins, and sugar values
- psframe = psframe.assign(Ratio =lambda x: (x["Protein"] / x["Sugars"] )) # add a new column with the protein and sugar ratio
- psframe = psframe.sort_values(by="Ratio", ascending = False) # Sort the dataframe based on the values
- psframe = psframe.replace([np.inf, -np.inf], np.nan).dropna(subset=["Ratio"], how="all").head(3) # Remove infinite and NaN values
- print(psframe)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement