Pandas - 命名系列中的一列并绘制它

Pandas - name a column in a Series and plot it

使用 Python 2.7 和笔记本,我试图显示一个简单的系列,如下所示:

year
2014    1
2015    3
2016    2
Name: mySeries, dtype: int64

我愿意:

  1. 命名第二列,我似乎不能用 s.columns = ['a','b'] 成功。我们如何做到这一点?
  2. 绘制这样写年份的结果。当我 运行 s.plot() 时,我得到 x 的年份,这很好,但我得到了奇怪的值:

感谢您的帮助!

如果有帮助,本系列来自以下代码:

df = pd.read_csv(file, usecols=['dates'], parse_dates=True)
df['dates'] = pd.to_datetime(df['date'])
df['year'] = pd.DatetimeIndex(df['dartes']).year
df

这给了我:

    dates   year
0   2015-05-05 14:21:00     2015
1   2014-06-06 14:22:00     2014
2   2015-05-05 14:14:00     2015

我做的是: s = pd.Series(df.groupby('year').size())

在之前投下你的索引:

df.set_index(df.index.astype(str),inplace=True)

那么你就会得到你所期望的。

对我来说,按 astype:

将索引转换为字符串
print s
y
2014    1
2015    3
2016    2
Name: mySeries, dtype: int64

s.index = s.index.astype(str)
s.plot()