When using the split method for DataFrame Views, an error appears: too many values ​​to unpack (expected 3). The task is to split the Date column into 3 columns year, month, day, then translate the date. What am I doing wrong, tell me, please? Code:

year, month, day = views.Date.str.split('/') 

enter image description here

  • What value in views.Date.str show. - Nikmoon
  • Your split results in a list of more than 3 items. - godva
  • Nikmoon, added screen. - Denis Novik
  • godva, that's the problem. There are three variables, and I expected that as a result of a split on "/" 3 columns will be obtained, but an error is returned. - Denis Novik
  • one
    views.Date = pd.to_datetime(views.Date) and you don’t need to divide anything and collect it again in datetime - MaxU

1 answer 1

Answering your question about .str.split(...) :

in .str.split(..., expand=False) by default expand=False , i.e. it will return pandas.Series where the items are lists:

 In [49]: df.date.str.split('/') Out[49]: 0 [2016, 01, 09] 1 [2016, 11, 11] 2 [2016, 12, 30] Name: date, dtype: object 

if you specify expand=True , then pandas.DataFrame will return:

 In [50]: df.date.str.split('/', expand=True) Out[50]: 0 1 2 0 2016 01 09 1 2016 11 11 2 2016 12 30 

you can still use .str.extract() and specify the column names:

 In [51]: df.date.str.extract(r'(?P<year>\d{4})[-/\.](?P<month>\d{2})[-/\.](?P<day>\d{2})', expand=True) Out[51]: year month day 0 2016 01 09 1 2016 11 11 2 2016 12 30 In [52]: split = df.date.str.extract(r'(?P<year>\d{4})[-/\.](?P<month>\d{2})[-/\.](?P<day>\d{2})', expand=True) In [53]: split Out[53]: year month day 0 2016 01 09 1 2016 11 11 2 2016 12 30 
  • MaxU, thanks! - Denis Novik