1

With Python 2.7 and notebook, I am trying to display a simple Series that looks like:

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

I would like to:

  1. Name the second column, I cant seem to succeed with s.columns = ['a','b']. How do we do this?
  2. Plot the result where the years are written as such. When I run s.plot() I get the years as x, which is good but I get weird values: Graph

Thanks for the help!

If it helps, this series comes from the following code:

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

which gives me:

    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

On which I do: s = pd.Series(df.groupby('year').size())

2
  • You have a Series so by definition it's 1-D so you can't assign multiple values to columns Commented May 10, 2016 at 9:30
  • that makes sense :) Thanks! Commented May 10, 2016 at 9:38

2 Answers 2

1

For me works cast index to string by astype:

print s
y
2014    1
2015    3
2016    2
Name: mySeries, dtype: int64

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

graph

Sign up to request clarification or add additional context in comments.

1 Comment

Great, thank you very much. Still not too familiar with Pandas! :)
0

just cast your index before :

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

then you will have what you expect.

1 Comment

Thank you, but unfortunately I am working with a Series ('Series' object has no attribute 'set_index'). I tried doing this on the df that my series comes from, but then I dont know how to groupy the index value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.