vmamontov

from_chat_DA_3

Mar 22nd, 2022 (edited)
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. import pandas as pd
  2. from datetime import datetime, timedelta
  3. import calendar
  4.  
  5. url = 'https://vk.com/s/v1/doc/Q8dE2aqabEx6LLW_FVGZLwrYk0afhuc1JKCC5PfvrlxBgaKyrGI'
  6.  
  7. df = pd.read_csv(url, names=['date', 'name', 'price'])
  8. df['dt'], df['days'], df['mean'] = None, 0, 0
  9.  
  10. # 1-ый блок
  11. for i in range(len(df)):
  12.     dt = pd.to_datetime(df.loc[i, 'date'], format='%d.%m.%Y')
  13.     days = calendar.monthrange(dt.year, dt.month)[1]
  14.  
  15.     df.loc[i, 'dt'] = dt
  16.     df.loc[i, 'days'] = days
  17.     df.loc[i, 'mean'] = df.loc[i, 'price'] / df.loc[i, 'days']
  18.  
  19. prin(df.head())
  20.  
  21. columns = ['date', 'name', 'mean']
  22. data_empty = {n:[] for n in columns}
  23. df2 = pd.DataFrame.from_dict(data_empty)
  24.  
  25. # 2-ой блок
  26. sites = list(df['name'].unique())
  27. for site in sites:
  28.     for i in range(len(df[df['name'] == site])):
  29.         days = df.loc[i, 'days']
  30.         price_mean = df.loc[i, 'mean']
  31.  
  32.         dlen = len(df2)
  33.  
  34.         for d in range(0, days):
  35.             df2.loc[dlen + d, 'date'] = df.loc[i, 'dt'] + timedelta(days=d)
  36.             df2.loc[dlen + d, 'name'] = site
  37.             df2.loc[dlen + d, 'mean'] = price_mean
  38.  
  39. df2
Add Comment
Please, Sign In to add comment