2

I need some help with a data types issue. I'm trying to convert a pandas dataframe, which looks like the following:

timestamp    number
2018-01-01     1
2018-02-01     0
2018-03-01     5
2018-04-01     0
2018-05-01     6

into a pandas series, which looks exactly like the dataframe, without the column names timestamp and number:

 2018-01-01     1
 2018-02-01     0
 2018-03-01     5
 2018-04-01     0
 2018-05-01     6

It shouldn't be difficult, but I'm having a little trouble figuring out the way to do it, as I'm a beginner in pandas.

It would be great to get some help with this issue.

Thank you very much in advance!

HRDSL
  • 711
  • 1
  • 5
  • 22
  • can you show how the output should look like? – anky Apr 19 '19 at 10:44
  • It should look just like the dataframe, but without the column names 'timestamp' and 'number' on it. – HRDSL Apr 19 '19 at 10:45
  • I am confused here. Why dont you want the names if you want a dataframe back? can you post the output in the question? – anky Apr 19 '19 at 10:46
  • I don't want a dataframe. I want a pandas Series, which doesn't have the names of the columns on it. I just edited the question, now you can check how I want the pandas Series to look like :) – HRDSL Apr 19 '19 at 10:47
  • 2
    Do you mean `df.set_index('timestamp')['number'].rename_axis(None)` ? – anky Apr 19 '19 at 10:50
  • See also https://stackoverflow.com/questions/39072010/how-to-convert-a-single-column-pandas-dataframe-into-series – Davide Fiocco Apr 19 '19 at 10:58
  • Yes! This worked, thanks :) – HRDSL Apr 19 '19 at 10:58

1 Answers1

3

IIUC, use:

df.set_index('timestamp')['number'].rename_axis(None)

2018-01-01    1
2018-02-01    0
2018-03-01    5
2018-04-01    0
2018-05-01    6
anky
  • 74,114
  • 11
  • 41
  • 70