Advertisement
hyor1

Exercise 4

Jan 2nd, 2025
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. import pandas as pd
  2.  
  3. df = pd.read_csv('/datasets/drink_preferences.csv')
  4. df = df.dropna(subset=['nonalcoholic_drink'])
  5. df_female = df[df['gender'] == 'Female']
  6.  
  7.  
  8. # Сгруппируйте и отсортируйте данные
  9. df_agg = (df_female
  10.           .groupby('nonalcoholic_drink')
  11.           .agg({'resp_id': 'nunique'})
  12.           .sort_values(by='resp_id', ascending=False)
  13.           .reset_index())
  14. df_agg = pd.DataFrame({'nonalcoholic_drink': ['Coffee', 'Green Tea', 'Black Tea',
  15.                                               'Apple Juice', 'Water', 'Gatorade',
  16.                                               'Dr. Pepper', 'Coke', 'Pepsi', 'Red Bull'],
  17.                       'resp_id': [25, 22, 21, 19, 19, 15, 12, 8, 7, 4]})
  18. print(df_agg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement