0

Trying to change a column from an array that has type to a list.

Tried changing it directly to a list, but it still comes up as a series after checking the type of it.

First I get the first 4 numbers to I can have just the year, then I create a new column in the table called year to hold that new data.

year = df['date'].str.extract(r'^(\d{4})')
df['year'] = pd.to_numeric(year)
df['year'].dtype
print(type(df['year']))

Want the type of 'year' to be a list. Thanks!

kalg1241
  • 13
  • 3
  • `type(df['year'].tolist())` – user_12 Nov 03 '19 at 17:20
  • tried the following and still comes up as a type(df['year'].tolist()) print(type(df['year'])) – kalg1241 Nov 03 '19 at 17:25
  • try `year = df['year'].tolist(); print(type(year))` – user_12 Nov 03 '19 at 17:28
  • 1
    By definition, every "column" in `pandas` dataframe is a `series`. You can save the values at a list with `df['year'].tolist()` or simply `list(df['year'])`, but if you save it back into the dataframe, of course it will still be a `series`. – Aryerez Nov 03 '19 at 17:29
  • Does this answer your question? [get list from pandas dataframe column](https://stackoverflow.com/questions/22341271/get-list-from-pandas-dataframe-column) – Alex Nov 03 '19 at 17:42

2 Answers2

1

If you want to get a list with years values into date column, you could try this:

import pandas as pd
df = pd.DataFrame({'date':['2019/01/02', '2018/02/03', '2017/03/04']})
year = df.date.str.extract(r'(\d{4})')[0].to_list()
print(f'type: {type(year)}: {year}')
# type: <class 'list'>: ['2019', '2018', '2017']

df.date.str.extract returns a new DataFrame with one row for each subject string, and one column for each group, then we take the first (only) group [0]

0

It seems pretty straightforward to turn a series into a list. The builtin list function works fine:

> df = pd.DataFrame({'date':['2019/01/02', '2018/02/03', '2017/03/04']})
> dates = list(df['date'])
> type(dates)
< <class 'list'>
> dates
< ['2019/01/02', '2018/02/03', '2017/03/04']
hd1
  • 33,938
  • 5
  • 80
  • 91