Python 在熊猫中使用索引绘制数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20084487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:29:37  来源:igfitidea点击:

Use index in pandas to plot data

pythonpandas

提问by paulchen

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

我有一个 pandas-Dataframe 并用于resample()计算平均值(例如每日或每月平均值)。这是一个小例子。

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')

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

我现在如何绘制monthly_mean?我如何设法使用我新创建的 DataFrame 的索引monthly_mean作为 x 轴?提前致谢。

采纳答案by Marius

You can use reset_indexto turn the index back into a column:

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

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

回答by Pablo Jadzinsky

Try this,

尝试这个,

monthly_mean.plot(y='A', use_index=True)

回答by Pablo Jadzinsky

Also,

还,

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

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