Pandas Task上 综合测试
一、2002 年-2018 年上海机动车拍照拍卖
问题
(1) 哪一次拍卖的中标率首次小于 5%?
load_path = '../data/'
df_car = pd.read_csv(load_path+'2002年-2018年上海机动车拍照拍卖.csv')
df_car
df_car['rate'] = df_car['Total number of license issued'] / df_car['Total number of applicants']
df_car.loc[df_car['rate']<0.05].head()
15年的5月
(2) 将第一列时间列拆分成两个列,一列为年份(格式为 20××),另一列为
月份(英语缩写),添加到列表作为第一第二列,并将原表第一列删除,
其他列依次向后顺延。
list_date = list(df_car['Date'].str.split('-'))
list_year = [x[0] for x in list_date]
list_month = [x[1] for x in list_date]
df_car['month'] = list_month
def year_change(x):
y = []
for i in x:
if len(i) == 1:
y.append('200'+i)
else:
y.append('20'+i)
return y
list_year1 = year_change(list_year)
df_car['year'] = list_year1
df_car.drop(columns='Date', inplace=True)
cols = list(df_car.columns)
cols.remove('month')
cols.remove('year')
cols.insert(0, 'month')
cols.insert(0, 'year')
cols
df_car = df_car[cols]
df_car
(3) 按年统计拍卖最低价的下列统计量:最大值、均值、0.75 分位数,要求
显示在同一张表上。
df_car.groupby('year')['lowest price '].agg(['max', 'mean', ('quantile(q=0.75)' ,lambda x: x.quantile(q=0.75))])
(4) 现在将表格行索引设为多级索引,外层为年份,内层为原表格第二至第
五列的变量名,列索引为月份。
df_car.set_index(['year', 'Total number of license issued', 'lowest price ', 'avg price', 'Total number of applicants'])
(5) 一般而言某个月最低价与上月最低价的差额,会与该月均值与上月均值
的差额具有相同的正负号,哪些拍卖时间不具有这个特点?
list_1 = []
list_2 = []
for i in range(202):
list_1.append(df_car.loc[i+1, 'lowest price '] - df_car.loc[i, 'lowest price '])
list_2.append(df_car.loc[i+1, 'avg price'] - df_car.loc[i, 'avg price'])
def pos_neg(list1, list2):
index = []
for i in range(202):
if list1[i] * list2[i] < 0:
index.append(i+1)
return index
df_car.loc[index_list]
(6) 将某一个月牌照发行量与其前两个月发行量均值的差额定义为发行增
益,最初的两个月用 0 填充,求发行增益极值出现的时间。
list_issue = []
for i in range(201):
list_issue.append