python简单的数据分析试题
时间: 2024-09-21 21:02:32 浏览: 42
Python是一种强大的数据处理和分析工具,常用于统计分析、数据清洗、可视化等任务。下面是一个简单的Python数据分析试题示例:
题目:假设你得到了一份包含销售记录的数据集,其中包括产品ID、日期、销售额和销量。你需要完成以下任务:
1. **数据加载**:使用pandas库读取CSV文件,并查看数据前几行。
```python
import pandas as pd
data = pd.read_csv('sales_data.csv')
print(data.head())
```
2. **数据清洗**:检查并处理缺失值,例如删除含有缺失值的记录或用平均值填充。
```python
data.dropna(inplace=True) # 删除所有含有缺失值的记录
# 或者 data.fillna(data.mean(), inplace=True)
```
3. **数据分组和聚合**:按月份计算每个月的总销售额和销量。
```python
monthly_stats = data.groupby(data['date'].dt.month)['sales', 'quantity'].sum()
print(monthly_stats)
```
4. **数据可视化**:使用matplotlib或seaborn绘制销售额随时间的变化趋势图。
```python
import matplotlib.pyplot as plt
plt.figure(figsize=(10,5))
monthly_sales.plot(kind='line')
plt.title('Monthly Sales Trend')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.show()
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)