python dataframe 求一组数据的累积密度,并画图
时间: 2024-09-06 21:01:37 浏览: 71
在Python的pandas库中,你可以使用`cumfreq()`函数计算DataFrame中数据的累积频率,然后结合matplotlib库来绘制累积密度曲线(CDF)。这里是一个简单的步骤:
首先,你需要导入必要的库:
```python
import pandas as pd
import matplotlib.pyplot as plt
```
假设你有一个名为df的数据框,其中包含需要分析的一列数据:
```python
# 示例数据
data = [random.sample(range(10), 100) for _ in range(10)] # 生成一些随机数据
df = pd.DataFrame({'column': data})
```
接下来,计算累积频率并存储在一个新列中:
```python
df['cumulative_density'] = df['column'].value_counts(normalize=True).cumsum()
```
最后,创建累积密度曲线图:
```python
plt.figure(figsize=(10,6))
plt.plot(df['column'], df['cumulative_density'], label='Cumulative Density')
plt.xlabel('Data Values')
plt.ylabel('Cumulative Frequency')
plt.title('Cumulative Density Function (CDF)')
plt.legend()
plt.show()
```
这将展示出该数据列的累积密度分布。
阅读全文
相关推荐

















