使用 pandas 索引来绘制数据 [英] Use index in pandas to plot data

查看:71
本文介绍了使用 pandas 索引来绘制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pandas-Dataframe,并使用resample()来计算均值(例如每日或每月均值). 这是一个小例子.

I have a pandas-Dataframe and use resample() to calculate means (e.g. daily or monthly means). Here is a small example.

import pandas as pd  
import numpy as np

dates = pd.date_range('1/1/2000', periods=100)
df = pd.DataFrame(np.random.randn(100, 1), index=dates, columns=['A'])

monthly_mean = df.resample('M', how='mean')

我现在该如何绘制monthly_mean? 如何管理将新创建的DataFrame monthly_mean的索引用作x轴? 预先感谢.

How do I plot the monthly_mean now? How do I manage to use the index of my new created DataFrame monthly_mean as the x-axis? Thanks in advance.

推荐答案

您可以使用reset_index将索引变回一列:

You can use reset_index to turn the index back into a column:

monthly_mean.reset_index().plot(x='index', y='A')

这篇关于使用 pandas 索引来绘制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆