Advertisement
DacCum

pandas

May 24th, 2022 (edited)
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.80 KB | None | 0 0
  1. #Задания 1
  2. import pandas as pd
  3.  
  4.  
  5. name_magazine = ("eqwdfszx", "bgfdvscz", "bvvtgf", "sbtygfdfvsd", "asdf")
  6. number_of_copies = [8000, 14000, 20000, 9000, 16000]
  7. price = [1100, 1000, 1200, 500, 1300]
  8.  
  9. magazine_dict = {k: v for k, v in zip(name_magazine, {k: v for k, v in zip(number_of_copies, price)}.items())}
  10.  
  11. df = pd.DataFrame.from_dict(magazine_dict, orient='index', columns=['number_of_copies', 'price'])
  12.  
  13. print(df)
  14.  
  15. # search
  16. print(f"\nbvvtgf price: {df.get('price').get('bvvtgf')}\n")
  17.  
  18. # агрегація
  19. print(f"Count: \n{df.count()}\n")
  20.  
  21. print(f"Mean: \n\t{df[['price']].mean()}")
  22. print(f"\t{df[['number_of_copies']].mean()}\n")
  23.  
  24. print(f"Min: \n\t{df[['price']].min()}")
  25. print(f"\t{df[['number_of_copies']].min()}\n")
  26.  
  27. print(f"Max: \n\t{df[['price']].max()}")
  28. print(f"\t{df[['number_of_copies']].max()}\n")
  29.  
  30. # групування
  31. print(f"{df.groupby('price').mean()}")
  32.  
  33.  
  34.  
  35. #Задания 2
  36. import calendar
  37. import pandas as pd
  38. import matplotlib.pyplot as plt
  39.  
  40.  
  41. fixed_df = pd.read_csv('comptagevelo2009.csv', parse_dates=['Date'], encoding='latin1', dayfirst=True, index_col='Date')
  42.  
  43. Berri1 = pd.DataFrame(fixed_df['Berri1'])
  44. Berri1['month'] = Berri1.index.month
  45.  
  46. Berri1_sum = Berri1.groupby(['month']).sum()
  47. Berri1_sum.index = ['January', 'February', 'March', 'April', 'May', 'June',
  48.                     'July', 'August', 'September', 'October', 'November', 'December']
  49.  
  50. print(f"The most popular month on the track Berri1: {Berri1_sum['Berri1'].idxmax()}")
  51.  
  52. print(f"The most popular day of the week on the track Berri1: {calendar.day_name[Berri1['Berri1'].idxmax().weekday()]}")
  53.  
  54. input("Press Enter to show diagrams.. ")
  55.  
  56. plt.style.use('ggplot')
  57. plt.rcParams['figure.figsize'] = (15, 5)
  58.  
  59. Berri1['Berri1'].plot(figsize=(15, 10))
  60.  
  61. Berri1_sum.plot(kind='bar')
  62.  
  63. plt.show()
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement