I have the following Series:
Period Ending
0 7/2017
1 4/2017
2 1/2017
3 10/2016
4 7/2016
5 4/2016
I want to convert the month to fewer options (call them quarters) .
For this operation I built a dictionary:
period_dict = {3: [1,2,3], 6:[4,5,6], 9:[7,8,9], 12:[10,11,12]}
Keys are quarters and values are months.
My idea is that if the month value in the pandas column is in a dictionary value (list) then replace it using the key value.
Output should look like this:
NEW Period Ending
0 9/2017
1 6/2017
2 3/2017
3 12/2016
4 9/2016
5 6/2016
I did lots of research but everything points to matching a key and replacing with a single dictionary value.
Also:
Python Pandas: How to replace a characters in a column of a dataframe?
Conceptually the closest I came up with is:
for k, v in period_dict.items():
if table['Period Ending'] in v:
table['Period Ending'].replace([i[0] for i in table['Period Ending']],k)
I get
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I appreciate any ideas.
EDIT 1
As part of the solution I was able to 'isolate' the month string but still do not know how to convert it using the dictionary.
print 'GET Month String'
''' Split date con / '''
# de https://stackoverflow.com/questions/27387415/how-would-i-get-everything-before-a-in-a-string-python
split_df = tabla['Period Ending'].astype(str).str.split('/')
split_date = split_df.str[0]
print split_date
print
Output:
GET Month String
0 7
1 4
2 1
3 10
4 7
5 4