temp = data.groupby(['position'])['unit_price'].mean().reset_index() data_pair = sorted([(row['position'], round(row['unit_price'] / 10000, 1)) for _, row in temp.iterrows()], key=lambda x: x[1], reverse=True)[:10] bar = (Bar(init_opts=opts.InitOpts(theme='dark')) .add_xaxis([x[0] for x in data_pair]) .add_yaxis('二手房均价', [x[1] for x in data_pair]) .set_series_opts(label_opts=opts.LabelOpts(is_show=True, font_style='italic'), itemstyle_opts=opts.ItemStyleOpts( color=JsCode("""new echarts.graphic.LinearGradient(0, 1, 0, 0, [{ offset: 0, color: 'rgb(0,206,209)' }, { offset: 1, color: 'rgb(218,165,32)' }])""")) ) .set_global_opts( title_opts=opts.TitleOpts(title="深圳二手房均价TOP 10地段"), legend_opts=opts.LegendOpts(is_show=False), tooltip_opts=opts.TooltipOpts(formatter='{b}:{c}万元')) ) bar.render_notebook()
时间: 2024-02-10 08:22:03 浏览: 69
这段代码是用 Python 生成一个条形图,用于显示深圳市二手房均价排名前十的地段信息。具体来说,代码首先对数据进行了分组,计算出每个地段的二手房均价;然后对这些数据进行排序,取出前十个地段的名称和均价;最后用 Pyecharts 库中的 Bar 类生成一个条形图,并将数据加入到图中。生成的条形图通过 render_notebook() 函数在 Jupyter Notebook 中进行渲染,以供用户查看。
相关问题
temp = data.groupby(['area'])['unit_price'].mean().reset_index() data_pair = [(row['area'], round(row['unit_price']/10000, 1)) for _, row in temp.iterrows()] map_ = (Map(init_opts=opts.InitOpts(theme='dark')) .add("二手房均价", data_pair, '深圳', is_roam=False) .set_series_opts(label_opts=opts.LabelOpts(is_show=True)) .set_global_opts( title_opts=opts.TitleOpts(title="深圳各行政区二手房均价"), legend_opts=opts.LegendOpts(is_show=False), tooltip_opts=opts.TooltipOpts(formatter='{b}:{c}万元'), visualmap_opts=opts.VisualMapOpts(min_=3, max_=10) ) ) map_.render_notebook()
这段代码是用来绘制深圳各行政区二手房均价的地图,首先对数据进行了分组并计算每个行政区的二手房均价,然后将结果转换成列表形式,接着使用 echarts 的 Map 组件进行绘制,其中 is_roam 参数设为 False 表示禁止拖拽和缩放地图,visualmap_opts 参数设置了颜色段以及最小值和最大值。最后使用 render_notebook() 方法将地图渲染在 Jupyter Notebook 中。
6、每个用户按周求和并差分(一周7天,年度分开),并求取差分结果的基本统计量,统计量同三。 res4 = pd.DataFrame() for col in data_t.columns: # 按周求和 data_weekly = data_t[col].resample('W').sum() # 差分 data_diff = data_weekly.diff(periods=1) # 去除第一个NaN值 data_diff = data_diff[1:] # 求取差分结果的基本统计量 res_temp = pd.DataFrame() res_temp['最大值'] = data_diff.max() res_temp['最小值'] = data_diff.min() res_temp['均值'] = data_diff.mean() res_temp['中位数'] = data_diff.median() res_temp['和'] = data_diff.sum() res_temp['方差'] = data_diff.var() res_temp['偏度'] = data_diff.skew() res_temp['峰度'] = data_diff.kurt() res4 = pd.concat([res4, res_temp.T], axis=1) res4.columns = data_t.columns print("每个用户按周求和并差分的基本统计量") print(res4)修改运行代码
修改后的运行代码如下:
```
import pandas as pd
# 创建示例数据
data_t = pd.DataFrame({'date': pd.date_range('20210101', periods=365),
'user1': [i%50 for i in range(365)],
'user2': [i%30 for i in range(365)]})
data_t = data_t.set_index('date')
# 对数据进行处理并计算基本统计量
res4 = pd.DataFrame()
for col in data_t.columns:
# 按周求和
data_weekly = data_t[col].resample('W').sum()
# 差分
data_diff = data_weekly.diff(periods=1)
# 去除第一个NaN值
data_diff = data_diff[1:]
# 求取差分结果的基本统计量
res_temp = pd.DataFrame()
res_temp['最大值'] = data_diff.max()
res_temp['最小值'] = data_diff.min()
res_temp['均值'] = data_diff.mean()
res_temp['中位数'] = data_diff.median()
res_temp['和'] = data_diff.sum()
res_temp['方差'] = data_diff.var()
res_temp['偏度'] = data_diff.skew()
res_temp['峰度'] = data_diff.kurt()
res4 = pd.concat([res4, res_temp.T], axis=1)
res4.columns = data_t.columns
# 输出结果
print("每个用户按周求和并差分的基本统计量:")
print(res4)
```
这段代码会首先创建一个示例数据(共365天,包含两个用户),然后按照上述要求对数据进行处理并计算基本统计量。注意需要将时间戳列设置为索引,并且确保数据类型正确。最后输出每个用户按周求和并差分的基本统计量。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)