Python下载课件
阅读原文时间:2023年07月08日阅读:2
from urllib.request import urlretrieve  # #下载网络文件到本地
import os
os.chdir("C:/Users/RankFan/Desktop/空间计量经济学")

for i in range(4, 6):
   url = f'http://www.mysmu.edu/faculty/zlyang/ECON6002_21-Web/Chap{i}-2021T2.pdf'
   print(url)
   name = url.split('/')[-1]
   print(name)
   urlretrieve(url, filename=name)

Dataframe Group-by

df = pd.DataFrame([{'name': 'Albert', 'store': 175, 'sales': 109, 'clients': 28},
                   {'name': 'Sharon', 'store': 129, 'sales': 208, 'clients': 248},
                   {'name': 'Albert', 'store': 275, 'sales': 524, 'clients': 78},
                   {'name': 'Sharon', 'store': 399, 'sales': 913, 'clients': 921},
                   {'name': 'Sharon', 'store': 851, 'sales': 482, 'clients': 527},
                   {'name': 'Albert', 'store': 974, 'sales': 314, 'clients': 323},
                   {'name': 'Pink', 'store': 868, 'sales': 532, 'clients': 273},
                   {'name': 'Angelina', 'store': 4, 'sales': 31, 'clients': 347}])

df.groupby('name')[['sales', 'clients']].sum()
df.groupby('name').get_group('Albert')

df.groupby('name').first()  # each first row of each group
df.groupby('name').last()
df.groupby('name').nth(1)  # 2nd Row of each group

df.sort_values(by='sales',
               ascending=False, ).groupby('name').first()  # Sort and get the first value of each group

df.groupby('name').groups  # return dict
df.groupby('name').size()  # size of group(s)
df.groupby('name').ngroups  # number of group(s)

df.groupby('name').agg({'store': 'count', 'sales': 'sum', 'clients': 'mean'})
df.groupby('name').agg(['sum', 'mean'])

df.groupby('name').plot()  # plot
df.groupby('name').transform(lambda x: x.fillna(x.mean()))  # transform