用python对手环的数据分析,数据中包含手环一周的模拟数据。文件类型为xlsx,分为基本信息、跑步、步行、骑行、健身、羽毛球和心率等记录工作表,数据文件为data1.xlsx。 要求: (1)统计一周中每天的步数、距离的情况 (2)统计每日消耗卡路里情况 (3)统计每日心率情况 (4)统计每日温度情况 (5)统计每日体温变化 (6)统计运动情况 (7)每日运动持续时长 (8)每次运动心率最大值、最小值、平均值 (9)要求使用折线图柱状图图表进行展示
时间: 2024-03-24 15:38:08 浏览: 262
好的,这是一个比较全面的分析,需要用到pandas、matplotlib等库。以下是代码实现:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 读取excel文件
df = pd.read_excel('data1.xlsx', sheet_name=None)
# 统计每天的步数和距离
step_distance = {}
for i in range(7):
step_distance[i] = {}
step_distance[i]['steps'] = df['步行'][df['步行']['date'].dt.dayofweek == i]['steps'].sum()
step_distance[i]['distance'] = df['步行'][df['步行']['date'].dt.dayofweek == i]['distance'].sum()
# 绘制每天的步数和距离折线图
plt.figure(figsize=(10, 6))
plt.plot(list(step_distance.keys()), [v['steps'] for v in step_distance.values()], label='steps')
plt.plot(list(step_distance.keys()), [v['distance'] for v in step_distance.values()], label='distance')
plt.xticks(list(step_distance.keys()), ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'])
plt.xlabel('Day of Week')
plt.ylabel('Steps and Distance')
plt.title('Steps and Distance by Day of Week')
plt.legend()
plt.show()
# 统计每日卡路里消耗情况
calories = df['步行'][['date', 'calories']].groupby('date').sum()
# 绘制每日卡路里消耗柱状图
plt.figure(figsize=(10, 6))
plt.bar(calories.index, calories['calories'])
plt.xlabel('Date')
plt.ylabel('Calories')
plt.title('Calories by Date')
plt.show()
# 统计每日心率情况
heart_rate = df['心率'][['date', 'heart_rate']].groupby('date').mean()
# 绘制每日心率折线图
plt.figure(figsize=(10, 6))
plt.plot(heart_rate.index, heart_rate['heart_rate'])
plt.xlabel('Date')
plt.ylabel('Heart Rate')
plt.title('Heart Rate by Date')
plt.show()
# 统计每日温度情况
temperature = df['基本信息'].iloc[0]['temperature']
temperature = pd.DataFrame({'date': pd.date_range(start='2021-01-01', periods=7), 'temperature': temperature})
# 绘制每日温度折线图
plt.figure(figsize=(10, 6))
plt.plot(temperature['date'], temperature['temperature'])
plt.xlabel('Date')
plt.ylabel('Temperature')
plt.title('Temperature by Date')
plt.show()
# 统计每日体温变化
body_temperature = df['基本信息'].iloc[0]['body_temperature']
body_temperature = pd.DataFrame({'date': pd.date_range(start='2021-01-01', periods=7), 'body_temperature': body_temperature})
# 绘制每日体温折线图
plt.figure(figsize=(10, 6))
plt.plot(body_temperature['date'], body_temperature['body_temperature'])
plt.xlabel('Date')
plt.ylabel('Body Temperature')
plt.title('Body Temperature by Date')
plt.show()
# 统计每日运动情况和持续时长
sports = {}
for key in df.keys():
if key in ['基本信息', '心率']:
continue
sports[key] = {}
sports[key]['duration'] = df[key]['duration'].sum()
# 绘制每日运动持续时长柱状图
plt.figure(figsize=(10, 6))
plt.bar(list(sports.keys()), [v['duration'] for v in sports.values()])
plt.xlabel('Sports')
plt.ylabel('Duration')
plt.title('Sports Duration')
plt.show()
# 统计每次运动心率最大值、最小值、平均值
max_min_mean_heart_rate = {}
for key in df.keys():
if key in ['基本信息', '心率']:
continue
max_min_mean_heart_rate[key] = {}
max_min_mean_heart_rate[key]['max'] = df[key]['heart_rate'].max()
max_min_mean_heart_rate[key]['min'] = df[key]['heart_rate'].min()
max_min_mean_heart_rate[key]['mean'] = df[key]['heart_rate'].mean()
# 绘制每次运动心率最大值、最小值、平均值柱状图
plt.figure(figsize=(10, 6))
plt.bar(list(max_min_mean_heart_rate.keys()), [v['max'] for v in max_min_mean_heart_rate.values()], label='Max')
plt.bar(list(max_min_mean_heart_rate.keys()), [v['min'] for v in max_min_mean_heart_rate.values()], bottom=[v['max'] for v in max_min_mean_heart_rate.values()], label='Min')
plt.bar(list(max_min_mean_heart_rate.keys()), [v['mean'] for v in max_min_mean_heart_rate.values()], bottom=[v['max'] + v['min'] for v in max_min_mean_heart_rate.values()], label='Mean')
plt.xlabel('Sports')
plt.ylabel('Heart Rate')
plt.title('Heart Rate by Sports')
plt.legend()
plt.show()
```
你可以根据需要修改代码中的参数和细节,比如图表的样式、颜色等。
阅读全文