- VisualStudio2022插件的安装及使用-编程手把手系列文章
- pprof-在现网场景怎么用
- C#实现的下拉多选框,下拉多选树,多级节点
- 【学习笔记】基础数据结构:猫树
因为还处于数据分析的学习阶段(野生Python学者),所以在kaggle这个网站找了两个数据集来给自己练练手.
import pandas as pd
import os
import matplotlib.pyplot as plt
import numpy as np
from random import choice
这里我下载了两个数据集第一个是关于咖啡的销售情况,第二个是关于Instagram这个网站1000名最受欢迎的博主的数据。 我就从咖啡的销售情况这个表入手,因为我看了第二个表实在是没有什么眉目去做T.T 数据集文件放在最下方有需要的可以自行下载.
# 读取目录内的文件
directory = r'C:\Users\Admin\Desktop\demo\练习'
files = os.listdir(directory)
print(files)
['coffee_result.csv', 'Instagram-Data.csv']
# 存放文件
files_list = []
for file in files:
if file.endswith('.csv'):
directory_file = fr'{directory}\{file}'
files_list.append(directory_file)
print(files_list)
['C:\\Users\\Admin\\Desktop\\demo\\练习\\coffee_result.csv', 'C:\\Users\\Admin\\Desktop\\demo\\练习\\Instagram-Data.csv']
# 读取需要的文件
df = pd.read_csv(files_list[0])
df.info()
df
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1464 entries, 0 to 1463
Data columns (total 6 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 date 1464 non-null object
1 datetime 1464 non-null object
2 cash_type 1464 non-null object
3 card 1375 non-null object
4 money 1464 non-null float64
5 coffee_name 1464 non-null object
dtypes: float64(1), object(5)
memory usage: 68.8+ KB
date | datetime | cash_type | card | money | coffee_name | |
---|---|---|---|---|---|---|
0 | 2024-03-01 | 2024-03-01 10:15:50.520 | card | ANON-0000-0000-0001 | 38.70 | Latte |
1 | 2024-03-01 | 2024-03-01 12:19:22.539 | card | ANON-0000-0000-0002 | 38.70 | Hot Chocolate |
2 | 2024-03-01 | 2024-03-01 12:20:18.089 | card | ANON-0000-0000-0002 | 38.70 | Hot Chocolate |
3 | 2024-03-01 | 2024-03-01 13:46:33.006 | card | ANON-0000-0000-0003 | 28.90 | Americano |
4 | 2024-03-01 | 2024-03-01 13:48:14.626 | card | ANON-0000-0000-0004 | 38.70 | Latte |
... | ... | ... | ... | ... | ... | ... |
1459 | 2024-09-05 | 2024-09-05 20:30:14.964 | card | ANON-0000-0000-0587 | 32.82 | Cappuccino |
1460 | 2024-09-05 | 2024-09-05 20:54:24.429 | card | ANON-0000-0000-0588 | 23.02 | Americano |
1461 | 2024-09-05 | 2024-09-05 20:55:31.429 | card | ANON-0000-0000-0588 | 32.82 | Cappuccino |
1462 | 2024-09-05 | 2024-09-05 21:26:28.836 | card | ANON-0000-0000-0040 | 27.92 | Americano with Milk |
1463 | 2024-09-05 | 2024-09-05 21:27:29.969 | card | ANON-0000-0000-0040 | 27.92 | Americano with Milk |
1464 rows × 6 columns 。
print(df['cash_type'].unique().tolist(),'\n',
len(df['card'].unique().tolist()),'\n',
df['coffee_name'].unique().tolist(),'\n',
len(df['coffee_name'].unique().tolist()))
['card', 'cash']
589
['Latte', 'Hot Chocolate', 'Americano', 'Americano with Milk', 'Cocoa', 'Cortado', 'Espresso', 'Cappuccino']
8
通过info返回的信息可以看到card列存在一些空值,那我就把空值处理一下 。
df[df['card'].isnull()]
date | datetime | cash_type | card | money | coffee_name | |
---|---|---|---|---|---|---|
12 | 2024-03-02 | 2024-03-02 10:30:35.668 | cash | NaN | 40.0 | Latte |
18 | 2024-03-03 | 2024-03-03 10:10:43.981 | cash | NaN | 40.0 | Latte |
41 | 2024-03-06 | 2024-03-06 12:30:27.089 | cash | NaN | 35.0 | Americano with Milk |
46 | 2024-03-07 | 2024-03-07 10:08:58.945 | cash | NaN | 40.0 | Latte |
49 | 2024-03-07 | 2024-03-07 11:25:43.977 | cash | NaN | 40.0 | Latte |
... | ... | ... | ... | ... | ... | ... |
657 | 2024-05-31 | 2024-05-31 09:23:58.791 | cash | NaN | 39.0 | Latte |
677 | 2024-06-01 | 2024-06-01 20:54:59.267 | cash | NaN | 39.0 | Cocoa |
685 | 2024-06-02 | 2024-06-02 22:43:10.636 | cash | NaN | 34.0 | Americano with Milk |
691 | 2024-06-03 | 2024-06-03 21:42:51.734 | cash | NaN | 34.0 | Americano with Milk |
692 | 2024-06-03 | 2024-06-03 21:43:37.471 | cash | NaN | 34.0 | Americano with Milk |
89 rows × 6 columns 。
空值是由支付类型为现金支付的那一列对应的行产生的 。
df['card'] = df['card'].fillna("-1")
df['card'].isnull().any()
np.False_
在info返回的信息看到date这一列的数值类型是对象,我就把它变成日期类型方便我自己后续操作 。
print(type(df.loc[1,'date']),type(df.loc[1,'datetime']))
df.loc[1,'date']
<class 'str'> <class 'str'>
'2024-03-01'
# 调整日期格式提取每行数据的月份
df['date'] = pd.to_datetime(df['date'])
df['datetime'] = pd.to_datetime(df['datetime'])
df['month'] = df['date'].dt.month
print(len(df['month'].unique()))
7
因为9月份的数据只有5天所以这个月就不纳入分析 。
# 查看每月的销量以及金额
df_six = df[df['month']!=9].copy()
month = df_six['month'].unique() # 把月份单独拎出
month_sales = df_six.groupby('month')['money'].count()
month_sum = df_six.groupby('month')['money'].sum()
figure,axes = plt.subplots(1,2,figsize=[16,8])
figure.suptitle("Month sales and sum",size=20)
ax1 = axes[0].bar(month,month_sales)
axes[0].set_xlabel('Month',size=16)
axes[0].set_ylabel('Count',size=16)
ax2 = axes[1].bar(month,month_sum)
axes[1].set_xlabel('Month',size=16)
axes[1].set_ylabel('Sum',size=16)
axes[0].bar_label(ax1,fmt="%d",label_type="center")
axes[1].bar_label(ax2,fmt="%d",label_type="center")
plt.subplots_adjust(wspace=0.5)
nrows,ncols = 2,4
figure3,axes = plt.subplots(nrows,ncols,figsize=[16,8],sharex=True,sharey=True)
coffee_month_sales = df_six.groupby(['month','coffee_name'])['money'].sum().reset_index(name='sum')
coffee_names = coffee_month_sales['coffee_name'].unique().tolist()
for idx,coffee_name in enumerate(coffee_names):
x,y = divmod(idx,ncols)
coffee_data = coffee_month_sales[coffee_month_sales['coffee_name']==coffee_name]
bars = axes[x,y].bar(coffee_data['month'],coffee_data['sum'])
axes[x,y].bar_label(bars,fmt="%d",label_type="center")
subtitle = f"{coffee_name} {int(coffee_data['sum'].sum())}"
axes[x,y].set_title(subtitle)
axes[x,y].set_xlabel('month',size=16)
axes[x,y].set_ylabel('sum',size=16)
figure3.suptitle('coffee month sales',size=20)
plt.tight_layout()
plt.subplots_adjust(wspace=0.5)
stati = df_six.groupby('coffee_name')['money'].count().reset_index(name='buyers')
stati.sort_values(by='buyers',ascending=True,inplace=True,ignore_index=True)
figure2,axes = plt.subplots(1,2,figsize=(16,8))
figure2.suptitle("Coffee audience number and proportion",size=20)
ax1 = axes[0].barh(stati.iloc[:,0],stati.iloc[:,1])
axes[0].bar_label(ax1,fmt="%d",label_type="center")
axes[0].set_ylabel("Kind",size=16)
axes[0].set_xlabel("Sum",size=16)
axes[1].pie(stati.iloc[:,1],labels=stati.iloc[:,0],autopct='%0.1f')
plt.subplots_adjust(wspace=0.5)
cardholder = df_six[df_six['card']!='-1'].copy()
cardholder['tag'] = 1
cardholder.drop(columns=['date','datetime','cash_type'],inplace=True)
cardholder['month_sum'] = cardholder.groupby('card')['tag'].transform('sum')
active_buyer = cardholder.groupby('card')['month_sum'].max().reset_index(name='buys')
active_buyer.sort_values(by='buys',inplace=True,ignore_index=True,ascending=False)
cardholder['money_sum'] = cardholder.groupby('card')['money'].transform('sum')
money_sum = cardholder.drop_duplicates(subset='card',ignore_index=True).copy()
money_sum.drop(columns=['money','coffee_name','month','tag','month_sum'],inplace=True)
money_sum.sort_values(by='money_sum',inplace=True,ignore_index=True,ascending=False)
result = pd.merge(active_buyer,money_sum)
print('总消费金额平均数:',result['money_sum'].mean(),'\n',
result.head(10))
总消费金额平均数: 75.29034111310592
card buys money_sum
0 ANON-0000-0000-0012 96 2772.44
1 ANON-0000-0000-0009 67 2343.98
2 ANON-0000-0000-0141 44 1101.08
3 ANON-0000-0000-0097 38 1189.34
4 ANON-0000-0000-0040 30 910.12
5 ANON-0000-0000-0003 27 744.04
6 ANON-0000-0000-0001 17 646.14
7 ANON-0000-0000-0134 13 470.76
8 ANON-0000-0000-0024 12 422.26
9 ANON-0000-0000-0059 12 337.00
通过打印的数据可以看到这算是最活跃的一批用户了 。
程度大致就做到这种情况了,谢谢观看,如果有什么好的方法也可以在评论区评论! 。
本文的代码以及思路根据以下链接做过参考: [https://tianchi.aliyun.com/notebook/464175?spm=a2c22.21852664.0.0.260f379cEBLg8w] 。
数据集下载链接: https://storage.googleapis.com/kagglesdsdata/datasets/5328600/9620471/index.csv?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=gcp-kaggle-com%40kaggle-161607.iam.gserviceaccount.com%2F20241019%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20241019T074949Z&X-Goog-Expires=259200&X-Goog-SignedHeaders=host&X-Goog-Signature=b136263c03183c66881a72c1df4dcc4a2a1523e5ece138f7d060a718d58823e983c90e018217c2557ea03fb1a3a75e8436055e3f86b1b97a684e6a74699d74de6bebfccaa2e27bc9193b9eda676e236c848ad62085afc36d408755a06f8062bdd88d20cb42da5fa397b785a39ff7b0b4a01465a0de0c348d4c2ef0a3aa9be989e8e2a897963172ec51bbff461277ede44290b60f5942988fd64801b089d7557e648a4e740fe9d5a64f9c27877a36e25a1cc0e4f4a61e5f8caf6325e5bff8dc8f5a0496464dbc7cc0c257610940deb59c864b368e9eb32fed796fb12f790d5197fa255bdf8bf96a06bb3c0b9540eebafaa438874a026f1ae86e045a7cc44e60c0 。
最后此篇关于kaggle数据集某咖啡店的营销数据分析的文章就讲到这里了,如果你想了解更多关于kaggle数据集某咖啡店的营销数据分析的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我想通过 API 检索 Facebook 业务管理器的每个结果成本和结果列的值。这可能吗? 最佳答案 是的,可以通过 Insight API 实现。 请求了解营销事件和 cost_per_action
我们正在尝试通过广告帐户洞察从 Marketing API 获取一些数据。 我们已找到所需的大部分字段,但找不到与网络界面中提供的“3 秒视频观看次数”属性相对应的字段。 以下字段可用: video_
我正在使用 Facebook 营销 API 并进行调用以获取属于 adaccount ( https://developers.facebook.com/docs/marketing-api/refe
我知道 fb 已经提供了一些关于 API 请求限制的文档 https://developers.facebook.com/docs/marketing-api/api-rate-limiting ,但
在 facebook 的文档中,在 "What's New" 中他们有一个标题为“使用广告复制 API 复制和重复使用广告”的链接。这导致 this .在这里,他们有 3 个链接,目前都已损坏。 我的
我是 Facebook 营销 API 的新手。我能够创建一个广告系列。 但我似乎无法创建 Adset。 我尝试使用 Javascript 发布到广告集: https://graph.facebook.
我最近一直在查看 Facebook Marketing API,我希望通过广告检索印象。我知道您过去可以使用 getStats( *fields*, *params* ) 但是在 2.4 中已弃用。
是否可以为批准广告的事件制作一个 facebook webhook。 当您制作广告时,它位于 pending review可能需要一些未定义时间的状态。我需要在广告获得批准后立即调用一些操作。 有适用
我目前正在创建一个服务器 CLI 应用程序,通过 Marketing API 检索 facebook 广告指标,它将作为服务器上的一项长期运行的服务来完成,定期执行此操作,无需用户输入,但我似乎找不到
我正在制作一堆不同的仪表板,显示来自不同事件的数据。 我担心#17 用户请求达到限制,因为我已经收到过好几次这样的消息了。 我正在使用 Facebook Marketing API 来获取特定营销事件
我正在尝试通过其 python 包 facebookapi 使用 Facebook Marketing API。 这是一个代码片段: from facebookads.adobjects.custom
具体,我正在尝试做一些与这个问题非常相似的事情(有同样的问题):FB Ads API (#17) User request limit reached 但是,我正尝试在 python 中执行此操作(并
我需要帮助。我在尝试将 ProductCatalog 添加到 Business Manager 时使用开发人员应用程序。之前我添加了 Multi Product,可以吗? $catalog = new
我想用 Python 编写查询,我希望在单个请求中包含所有广告系列效果详细信息。 如何在 Python Query 中转换 below graph api 请求? //act_/campaigns?f
遵循此处的文档 https://developers.facebook.com/docs/marketing-api/reference/ad-campaign-group/insights/ 我正在
我们有基于 Java 的 CRM,并希望集成 Facebook Activity 管理。我检查了 facebook marketing api,他们提供了基于 PHP 和 Python 的 SDK。我
我正在尝试按广告集状态过滤广告集见解,但是当我添加状态过滤器时,我得到一个空数据集: curl "https://graph.facebook.com/v2.7/act_/insights?field
我正在尝试按广告集状态过滤广告集见解,但是当我添加状态过滤器时,我得到一个空数据集: curl "https://graph.facebook.com/v2.7/act_/insights?field
我正在使用 Facebook marketing API v2.8(首选JAVA) 我正在尝试使用以下规则使用 Facebook Pixel 创建自定义网站受众: 字符串规则 = "{\"url\
所以我正在尽最大努力通过 Facebook API 导航。我需要创建一个脚本,每天将我公司的事件信息下载为 csv 文件,这样我就可以使用另一个脚本轻松地将信息上传到我们的数据库。 我终于有了可以将信
我是一名优秀的程序员,十分优秀!