0

I have a data in a column in a csv format and want to append it in series in python that it can be displayed like:

series=[12,13,12,22,34,21]

How can it be done? Should I do it via read_csv and then somehow transform it to series or it is a another way?

Thanks!

MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
user3882036
  • 98
  • 1
  • 8
  • Please provide more information: is your column of interest the only column in your csv or just one of many? How do you read the csv, self-implemented or using a certain package? Is the series to be entirely build from the csv or are there previous data in the series where to append the csv data? – C. Rahn Aug 30 '16 at 22:47

2 Answers2

1

IIUC and you are talking about pandas - you can use squeeze parameter:

In [86]: import pandas as pd

In [87]: s = pd.read_csv('d:/temp/.data/1.csv', header=None, squeeze=True)

In [88]: s
Out[88]:
0    12
1    13
2    12
3    22
4    34
5    21
Name: 0, dtype: int64

as a vanilla Python list:

In [89]: s.tolist()
Out[89]: [12, 13, 12, 22, 34, 21]

from docs:

squeeze : boolean, default False

If the parsed data only contains one column then return a Series

Community
  • 1
  • 1
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
0

Try pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False) after reading your data as data frame with read_csv. Pandas documentation

Another possibility is to use iloc function, when you deal with a row, not a column. See here: Convert pandas data frame to series

Community
  • 1
  • 1
vlad.rad
  • 1,055
  • 2
  • 10
  • 28