Advertisement
Guest User

Untitled

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