d = data_2_1.groupby('校园卡号').agg({'消费次数':np.size,'消费金额':np.sum})[['消费金额','消费次数']] # 封装箱线图 boxplot(data = d) # 依据箱线图去除异常数据 d = d[ (d['消费金额'] < 800) & (d['消费次数'] < 180)] print(d.mean()) data_3_zhuanye = data_2_1.query("专业名称 in ['18国际金融','18会计','18商务日语']") a = data_3_zhuanye.groupby(['专业名称','性别'])['消费金额'].mean().unstack() a = np.round(a,2) # 小数点两位且四舍五入 with sns.color_palette('Blues_r'): bar = a.plot.bar() plt.xticks(rotation =0) plt.title('平均每次刷卡金额') for i in bar.containers: plt.bar_label(i)
时间: 2023-09-24 12:07:08 浏览: 35
这段代码看起来是一个数据分析的过程。首先,将数据按照校园卡号分组,统计每个校园卡号的消费次数和消费金额,然后绘制箱线图以便去除异常数据。接着,将统计结果中消费金额小于800元且消费次数小于180的数据保留下来,并计算这些数据的平均值。最后,从原始数据中筛选出专业名称为'18国际金融'、'18会计'、'18商务日语'的记录,并按照性别和消费金额进行分组计算平均值,将结果绘制成条形图。其中,使用了Python中的pandas和seaborn库。
相关问题
修改报错的语句:words_count=df_all_words.groupby(by=['all_words'])['all_words'].agg({"count":numpy.size}) print(words_count) words_count=words_count.reset_index().sort_values(by=["count"],ascending=False) words_count.head()
您好,根据您提供的代码,可能会提示以下报错:
```
FutureWarning: Using a dict on a groupby is deprecated and will be removed in a future version of pandas.
```
这是因为 `agg` 函数的参数应该是一个函数或函数列表,而不是字典。为了避免这个警告和报错,您可以将字典改为函数 `numpy.size`,如下所示:
```
words_count = df_all_words.groupby(by=['all_words'])['all_words'].agg(numpy.size).rename('count')
print(words_count)
words_count = words_count.reset_index().sort_values(by=["count"], ascending=False)
words_count.head()
```
这样就可以正确地计算每个单词出现的次数并按照出现次数从大到小排序了。
if __name__ == "__main__": TestDTW.test_cluster_effect_agg()解释一下
这是一个 Python 代码片段,其中的 if __name__ == "__main__": 表示这个代码是在直接运行时才会执行,而不是在被其他模块导入时执行。TestDTW.test_cluster_effect_agg() 是调用了 TestDTW 类中的 test_cluster_effect_agg() 方法。这个方法可能是用来测试 DTW 聚类效果的。