import pandas as pd
from pandas import DataFrame
path = r'E:\预处理实训报告\userlostprob - 副本.csv'
data = pd.read_csv(path)
# print (data.info())
lyhdata = data.loc[:,('sampleid','arrival','consuming_capacity','avgprice','starprefer')]
# print(lyhdata)
zcy3 = lyhdata['arrival'].isin(['2016/5/18'])
# print(zcy3)
# print(lyhdata['consuming_capacity'].mean())
qq = lyhdata['consuming_capacity']
# print(qq.fillna(qq.mean()))
grouped_single=lyhdata.groupby('arrival')
# print(grouped_single)
grouped_single.get_group('2016/5/18').head()
grouped_mul = lyhdata.groupby(['arrival'])
grouped_mul.get_group(('2016/5/18'))
# print(grouped_mul)
data_df = DataFrame()
data_df.head()
qxe_list = []
zcy4_list = []
zxe_list = []
qqq_list = []
qwe_list = []
z_list = []
x_list = []
for zcy4,group in grouped_single:
qxe = group['avgprice'].max()
zxe = group['starprefer'].mean()
qqq = group['avgprice'].isnull().sum()
qwe = group['starprefer'].isnull().sum()
z = qq.quantile(0.25)
x = qq.quantile(0.75)
data_list = [qxe, zxe, qqq, qwe,z,x]
#print(x,z)
new_df = DataFrame([data_list])
# print(qxe)
data_df = data_df.append(new_df)
# qxe_list.append(qxe)
zcy4_list.append(zcy4)
data_df.index = zcy4_list
data_df.columns = ['ave_price','starprefer_mean','avgprice','starprefer_sum','z','x']
data_df.to_csv('data.csv')
import pandas as pd
data = pd.read_csv('F:\\1.csv')
# 创建一个子数据集,子数据集是你的姓名,然后子数据集中放入下列字段:
#‘样本ID’、‘到达’、‘消费量’、‘平均价格’、‘喜欢’
sjj = data.loc[:,('sampleid','arrival','consuming_capacity','avgprice','starprefer')]
# 1求出酒店入住日期列表,统计每个日期都有多少人入住
one = data.groupby(['arrival']).size()
# 2求出每天的‘avgprice’字段中的最高值。
two = data[['arrival','avgprice']].groupby(by='arrival',as_index='arrival').max()
# 3求出每天的‘starprefer’的平均值。
three = data[['arrival','starprefer']].groupby(by='arrival',as_index='arrival').mean()
# 4统计子数据集中‘avgprice’, ‘starprefer’的缺失率。
four = data.groupby(by='arrival',as_index='arrival')['avgprice','starprefer'].agg('count').add_suffix("_count")
# 5填补cosuming_capacity中的缺失值,以均值填补的方式处理,然后求出每天的25%分位值和75%分位值。
q = data[['arrival','consuming_capacity']].groupby(by='arrival',as_index='arrival').quantile(q=0.25).add_suffix("_0.25")
w = data[['arrival','consuming_capacity']].groupby(by='arrival',as_index='arrival').quantile(q=0.75).add_suffix("_0.75")
five = pd.concat([q,w],axis=0)
# 6构建一个新的数据集,数据集中放入2,3,4,5中计算出的数值(共6个字段),并且以arrival作为索引。
test = pd.DataFrame(data=pd.concat([two,three,four,five],axis=0))
print(test)
# https://blog.csdn.net/weixin_43064185/article/details/107457178
# https://www.jianshu.com/p/463d1f89e229
# https://blog.csdn.net/qq_39521554/article/details/79574561
# https://www.runoob.com/pandas/pandas-dataframe.html
|