Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- from datetime import datetime, timedelta
- import calendar
- url = 'https://vk.com/s/v1/doc/Q8dE2aqabEx6LLW_FVGZLwrYk0afhuc1JKCC5PfvrlxBgaKyrGI'
- df = pd.read_csv(url, names=['date', 'name', 'price'])
- df['dt'], df['days'], df['mean'] = None, 0, 0
- # 1-ый блок
- for i in range(len(df)):
- dt = pd.to_datetime(df.loc[i, 'date'], format='%d.%m.%Y')
- days = calendar.monthrange(dt.year, dt.month)[1]
- df.loc[i, 'dt'] = dt
- df.loc[i, 'days'] = days
- df.loc[i, 'mean'] = df.loc[i, 'price'] / df.loc[i, 'days']
- prin(df.head())
- columns = ['date', 'name', 'mean']
- data_empty = {n:[] for n in columns}
- df2 = pd.DataFrame.from_dict(data_empty)
- # 2-ой блок
- sites = list(df['name'].unique())
- for site in sites:
- for i in range(len(df[df['name'] == site])):
- days = df.loc[i, 'days']
- price_mean = df.loc[i, 'mean']
- dlen = len(df2)
- for d in range(0, days):
- df2.loc[dlen + d, 'date'] = df.loc[i, 'dt'] + timedelta(days=d)
- df2.loc[dlen + d, 'name'] = site
- df2.loc[dlen + d, 'mean'] = price_mean
- df2
Add Comment
Please, Sign In to add comment