免費(fèi)名字設(shè)計(jì)logo網(wǎng)站查網(wǎng)站
一、實(shí)驗(yàn)數(shù)據(jù)
數(shù)據(jù)集:“加油站數(shù)據(jù).xls”
數(shù)據(jù)集介紹:該表記錄了用戶在11月和12月一天24小時(shí)內(nèi)的加油信息,包括:持卡人標(biāo)識(shí)(cardholder)、卡號(hào)(cardno)、加油站網(wǎng)點(diǎn)號(hào)(nodeno)、加油時(shí)間(opetime)、加油升數(shù)(liter)、金額(amount)
?? ??? ? ?
?
二、實(shí)驗(yàn)任務(wù)
a. 按照加油時(shí)間順序?qū)Ρ?中的數(shù)據(jù)進(jìn)行排序;
b. 統(tǒng)計(jì)加油站的站點(diǎn)數(shù);統(tǒng)計(jì)用戶總數(shù)(相同卡號(hào)按照同一人計(jì)算);統(tǒng)計(jì)17:30~18:30期間加油的人數(shù)和平均加油量,以及該時(shí)間段的加油總量在一天中的占比;
c. 按照月份可視化展示用戶卡號(hào)為“1000113200000151566”的加油量;分析用戶“1000113200000151566”常去的加油站點(diǎn)和通常的加油時(shí)間;根據(jù)加油時(shí)間、加油量、加油站點(diǎn)等因素分析該用戶2007年是否有行為異常的時(shí)間,并進(jìn)行解釋說明。
d. 分析國(guó)慶節(jié)是否可以拉動(dòng)加油站的經(jīng)濟(jì)增長(zhǎng)(提示:可以跟整體和當(dāng)月加油量的均值和方差進(jìn)行對(duì)比);?
e. 某用戶周六9:00~19:00時(shí)間自由,你推薦他什么時(shí)間去哪個(gè)加油站加油,為什么;
f. 分析2007年期間,加油站點(diǎn)“32000966”的汽油價(jià)格波動(dòng)情況;
三、前期工作
數(shù)據(jù)預(yù)處理
數(shù)據(jù)整合
import pandas as pd# 讀取整個(gè)Excel文件
xls = pd.ExcelFile('D:/MecLearning/加油站數(shù)據(jù).xls')# 創(chuàng)建一個(gè)空的DataFrame,用于存儲(chǔ)所有表頁(yè)的內(nèi)容
combined_data = pd.DataFrame()# 遍歷Excel文件的每個(gè)表頁(yè),并將其內(nèi)容合并到combined_data中
for sheet_name in xls.sheet_names:df = pd.read_excel(xls, sheet_name)combined_data = pd.concat([combined_data, df], ignore_index=True)# 將合并后的數(shù)據(jù)寫入一個(gè)新的Excel文件
combined_data.to_excel('D:/MecLearning/合并后的數(shù)據(jù).xlsx', index=False)print("數(shù)據(jù)已整合并寫入新的Excel文件 '合并后的數(shù)據(jù).xlsx'")
?時(shí)間列分割
import pandas as pd# 讀取原始Excel文件
df = pd.read_excel('D:/MecLearning/合并后的數(shù)據(jù).xlsx')# 將opetime列分割成年月日和時(shí)間兩列
df['Date'] = df['opetime'].str.split().str[0] # 提取年月日部分
df['Time'] = df['opetime'].str.split().str[1] # 提取時(shí)間部分# 將修改后的DataFrame保存到原始文件中
df.to_excel('D:/MecLearning/合并后的數(shù)據(jù).xlsx', index=False)print("修改后的Date和Time列已保存到原始文件中")
?
正式解題
a.按照加油時(shí)間順序?qū)?shù)據(jù)進(jìn)行排序:
import pandas as pd# 讀取Excel文件
df = pd.read_excel('D:/MecLearning/加油站數(shù)據(jù).xls')# 按照加油時(shí)間排序
df.sort_values(by='opetime', inplace=True)# 打印排序后的數(shù)據(jù)
print(df)
b.統(tǒng)計(jì)加油站的站點(diǎn)數(shù),統(tǒng)計(jì)用戶總數(shù),統(tǒng)計(jì)17:30~18:30期間加油的人數(shù)和平均加油量,以及該時(shí)間段的加油總量在一天中的占比:
# 統(tǒng)計(jì)加油站站點(diǎn)數(shù)
station_count = df['nodeno'].nunique()# 統(tǒng)計(jì)用戶總數(shù)
user_count = df['cardno'].nunique()# 過濾出17:30~18:30期間的數(shù)據(jù)
filtered_data = df[(df['opetime'] >= '17:30:00') & (df['opetime'] <= '18:30:00')]# 統(tǒng)計(jì)該時(shí)間段加油的人數(shù)和平均加油量
people_count = filtered_data['cardholder'].nunique()
average_liter = filtered_data['liter'].mean()# 計(jì)算該時(shí)間段的加油總量占比
total_liter = filtered_data['liter'].sum()
total_day_liter = df['liter'].sum()
percentage = (total_liter / total_day_liter) * 100print(f"加油站站點(diǎn)數(shù):{station_count}")
print(f"用戶總數(shù):{user_count}")
print(f"17:30~18:30期間加油的人數(shù):{people_count}")
print(f"平均加油量:{average_liter:.2f}升")
print(f"該時(shí)間段的加油總量占比:{percentage:.2f}%")
?
c.可視化展示用戶卡號(hào)為“1000113200000151566”的加油量,分析用戶常去的加油站點(diǎn)和通常的加油時(shí)間,并分析是否有異常時(shí)間段:
import pandas as pd
import matplotlib.pyplot as plt
import warnings# 忽略特定警告
warnings.filterwarnings("ignore", category=UserWarning, module="matplotlib")# 讀取Excel文件
df = pd.read_excel('D:/MecLearning/合并后的數(shù)據(jù).xlsx')# 按照加油時(shí)間排序
df.sort_values(by='opetime', inplace=True)# 過濾出指定用戶的數(shù)據(jù),并明確創(chuàng)建一個(gè)副本
user_data = df[df['cardno'] == 1000113200000160000].copy()import matplotlib.pyplot as plt
import matplotlib.font_manager as fm# 查找支持的中文字體
font_path = fm.findSystemFonts(fontpaths=None, fontext="ttf")
for font in font_path:if "SimHei" in font:plt.rcParams['font.sans-serif'] = [fm.FontProperties(fname=font).get_name()]plt.rcParams['axes.unicode_minus'] = Falsebreak# 繪圖代碼
# 檢查用戶數(shù)據(jù)是否為空
if not user_data.empty:# a. 可視化用戶加油量按月份user_data['opetime'] = pd.to_datetime(user_data['opetime']) # 轉(zhuǎn)換為日期時(shí)間類型user_data['month'] = user_data['opetime'].dt.month # 提取月份monthly_data = user_data.groupby('month')['liter'].sum() # 按月份統(tǒng)計(jì)加油量monthly_data.plot(kind='bar')plt.xlabel('month')plt.ylabel('liter')plt.title('User 1000113200000151566 liter')plt.show()# b. 分析用戶常去的加油站點(diǎn)和通常的加油時(shí)間frequent_station = user_data['nodeno'].mode().values[0]frequent_time = user_data['opetime'].mode().values[0]print(f"常去的加油站點(diǎn):{frequent_station}")print(f"通常的加油時(shí)間:{frequent_time}")# c. 根據(jù)因素分析異常時(shí)間段# 這部分需要進(jìn)一步的數(shù)據(jù)分析和領(lǐng)域知識(shí),以確定異常時(shí)間段# 查找超出正常范圍的加油量mean_liter = user_data['liter'].mean()std_liter = user_data['liter'].std()threshold = mean_liter + 2 * std_liter # 將2倍標(biāo)準(zhǔn)差作為異常閾值abnormal_periods = user_data[user_data['liter'] > threshold]if not abnormal_periods.empty:print("發(fā)現(xiàn)異常時(shí)間段:")print(abnormal_periods)else:print("未找到用戶1000113200000151566的加油記錄")
?
?
d. 分析國(guó)慶節(jié)是否可以拉動(dòng)加油站的經(jīng)濟(jì)增長(zhǎng),可以比較國(guó)慶節(jié)期間的加油量與整體和當(dāng)月加油量的均值和方差:
import pandas as pd
import matplotlib.pyplot as plt
import warnings# 忽略特定警告
warnings.filterwarnings("ignore", category=UserWarning, module="matplotlib")# 讀取Excel文件
df = pd.read_excel('D:/MecLearning/合并后的數(shù)據(jù).xlsx')# 假設(shè)國(guó)慶節(jié)日期范圍為 '2007-10-01' 到 '2007-10-07'
national_day_data = df[(df['Date'] >= '2007-11-01') & (df['Date'] <= '2007-11-07')]# 計(jì)算國(guó)慶節(jié)期間的加油量均值和方差
national_day_mean = national_day_data['liter'].mean()
national_day_var = national_day_data['liter'].var()# 計(jì)算整體加油量均值和方差
overall_mean = df['liter'].mean()
overall_var = df['liter'].var()# 計(jì)算當(dāng)月加油量均值和方差
current_month_data = df[df['Date'].str.startswith('2007-11')]
current_month_mean = current_month_data['liter'].mean()
current_month_var = current_month_data['liter'].var()# 打印比較結(jié)果
print(f"國(guó)慶節(jié)期間的加油量均值:{national_day_mean:.2f}升")
print(f"國(guó)慶節(jié)期間的加油量方差:{national_day_var:.2f}")
print(f"整體加油量均值:{overall_mean:.2f}升")
print(f"整體加油量方差:{overall_var:.2f}")
print(f"當(dāng)月加油量均值:{current_month_mean:.2f}升")
print(f"當(dāng)月加油量方差:{current_month_var:.2f}")
?
e. 推薦某用戶周六9:00~19:00時(shí)間自由的加油時(shí)間和加油站點(diǎn):
# 過濾出周六的數(shù)據(jù)
saturday_data = df[df['opetime'].dt.weekday == 5]# 過濾出9:00~19:00的數(shù)據(jù)
saturday_data = saturday_data[(saturday_data['opetime'] >= '09:00:00') & (saturday_data['opetime'] <= '19:00:00')]# 找出用戶最常去的加油站點(diǎn)
frequent_station = saturday_data['nodeno'].mode().values[0]# 找出平均加油量最高的時(shí)間段
best_time = saturday_data.groupby('opetime')['liter'].mean().idxmax()print(f"推薦加油時(shí)間:{best_time}")
print(f"推薦加油站點(diǎn):{frequent_station}")
f. 分析加油站點(diǎn)“32000966”的汽油價(jià)格波動(dòng)情況:
計(jì)算price
import pandas as pd
import matplotlib.pyplot as plt
import warnings# 忽略特定警告
warnings.filterwarnings("ignore", category=UserWarning, module="matplotlib")# 讀取Excel文件
df = pd.read_excel('D:/MecLearning/合并后的數(shù)據(jù).xlsx')# 檢查是否包含 'amount' 和 'liter' 列
if 'amount' in df.columns and 'liter' in df.columns:# 新增 'price' 列df['price'] = df['amount'] / df['liter']# 保存帶有 'price' 列的新數(shù)據(jù)框到Excel文件df.to_excel('D:/MecLearning/合并后的數(shù)據(jù)_with_price.xlsx', index=False)
else:print("數(shù)據(jù)中缺少 'amount' 或 'liter' 列。")
?分析加油站點(diǎn)“32000966”的汽油價(jià)格波動(dòng)
import pandas as pd
import matplotlib.pyplot as plt
import warnings# 忽略特定警告
warnings.filterwarnings("ignore", category=UserWarning, module="matplotlib")# 讀取Excel文件
df = pd.read_excel('D:/MecLearning/合并后的數(shù)據(jù)_with_price.xlsx')# 假設(shè)數(shù)據(jù)集包含汽油價(jià)格字段為'price'
station_data = df[df['nodeno'] == '32000966']
station_data['Date'] = pd.to_datetime(station_data['Date']) # 轉(zhuǎn)換為日期時(shí)間類型
station_data.sort_values(by='Date', inplace=True) # 按照加油時(shí)間排序import matplotlib.pyplot as plt
import matplotlib.dates as mdates# 設(shè)置日期時(shí)間刻度格式
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.gca().xaxis.set_major_locator(mdates.MonthLocator(interval=1)) # 設(shè)置刻度間隔為一個(gè)月# 繪制汽油價(jià)格的折線圖
plt.plot(station_data['Date'], station_data['price'])
plt.xlabel('日期')
plt.ylabel('汽油價(jià)格')
plt.title('2007年加油站點(diǎn)32000966汽油價(jià)格波動(dòng)情況')
plt.xticks(rotation=45) # 旋轉(zhuǎn)x軸標(biāo)簽,以防止重疊
plt.grid() # 添加網(wǎng)格線
plt.show()