Reputation: 610
I have a dataset that contain 4 columns:
"Date"
, "Num_week"
, "Calendar"
df.head()
looks like:
Date Num_week Calendar
412 2012-01-01 1 (2012, 1)
413 2012-01-02 2 (2012, 1)
414 2012-01-03 2 (2012, 1)
415 2012-01-04 2 (2012, 1)
416 2012-01-05 2 (2012, 1)
I sord values in column: sorted(list(set(date_week['calendar'])))
result:
['(2012, 1)',
'(2012, 10)',
'(2012, 11)',
'(2012, 12)',
'(2012, 2)',
'(2012, 3)', etc.
And I try to get year and month separeted in loop.
for year, month in list(set(date_week['calendar'])):
print(year, month)
But get ValueError:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-168-cf01e0d2888e> in <module>()
----> 1 for year, month in list(set(date_week['calendar'])):
2 print(year, month)
ValueError: too many values to unpack (expected 2)
I've already tried use .items()
and get wrong result.
Could you help me to deal with this problem?
Upvotes: 1
Views: 2671
Reputation: 4590
date_week
Date Num_week Calender
412 2012-01-01 1 (2012, 1)
413 2012-01-02 2 (2012, 1)
414 2012-01-03 2 (2012, 1)
415 2012-01-04 2 (2012, 1)
416 2012-01-05 2 (2012, 1)
Solution 1: Get output in list
l = list(zip(*df['Calender']))
[(2012, 2012, 2012, 2012, 2012), (1, 1, 1, 1, 1)]
OR
y,m = list(zip(*df['Calender']))
year = list(y)
month = list(m)
Output:
print(year)
[2012, 2012, 2012, 2012, 2012]
print(month)
[1, 1, 1, 1, 1]
Solution 2: You can create separate dataframe columns
ym = pd.DataFrame(df['Calender'].values.tolist(), columns=['year','month'], index=date_week.index)
ym
year month
412 2012 1
413 2012 1
414 2012 1
415 2012 1
416 2012 1
and merge with existing dataframe
date_week_new = pd.concat([df, ym],axis=1)
date_week_new
Date Num_week Calender year month
412 2012-01-01 1 (2012, 1) 2012 1
413 2012-01-02 2 (2012, 1) 2012 1
414 2012-01-03 2 (2012, 1) 2012 1
415 2012-01-04 2 (2012, 1) 2012 1
416 2012-01-05 2 (2012, 1) 2012 1
Upvotes: 1
Reputation: 863291
Problem is there are no tuples, but string repr of tuples, so need converting first:
import ast
date_week['Calendar'] = date_week['Calendar'].apply(ast.literal_eval)
So possible use your solution or alternative:
for year, month in date_week['Calendar'].unique():
print(year, month)
2012 1
EDIT: Alternative solution with Series.str.findall
and converting to tuples:
date_week['Calendar'] = date_week['Calendar'].str.findall('\d+').apply(tuple)
print (date_week)
Date Num_week Calendar
412 2012-01-01 1 (2012, 1)
413 2012-01-02 2 (2012, 1)
414 2012-01-03 2 (2012, 1)
415 2012-01-04 2 (2012, 1)
416 2012-01-05 2 (2012, 1)
Upvotes: 1