<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Import the fredapi module. Note that I have set my api key to the environment variable FRED_API_KEY. You can also pass your key explicitly.
Step1:  Import pandas and several display and plotting options
Step2:  If you already know the series ID you want (say by searching on the FRED website), you can fetch data easily into a pandas Series
Step3:  You can also easily fetch the meta data about any series
Step4:  You can also get a set of series IDs programmatically by release or category IDs. Several sorting options are also available. On the FRED website I know that the release ID 175 contains some personal income data. Let's fetch 5 most popular series in that set.
Step5:  You can also search by categories. On the FRED website I see that category 101 contains data about Consumer Credit.
Step6:  As a example let's fetch the personal income data. Release 151 looks quite intersting
Step7:  I noticed that the data is mostly organized by state, except for a few that are by BEA region. We can use pandas to easily select the seires we want
Step8:  looks good, we got the data series for all 50 states here
<ASSISTANT_TASK:>
Python Code:
from fredapi import Fred
fred = Fred()
Explanation: Import the fredapi module. Note that I have set my api key to the environment variable FRED_API_KEY. You can also pass your key explicitly.
End of explanation
import pandas as pd
pd.options.display.max_colwidth = 60
%matplotlib inline
import matplotlib.pyplot as plt
from IPython.core.pylabtools import figsize
figsize(20, 5)
Explanation: Import pandas and several display and plotting options
End of explanation
s = fred.get_series('SP500', observation_start='2014-09-02', observation_end='2014-09-05')
s.tail()
s = fred.get_series('SP500', observation_start='1/31/2014')
s.tail()
Explanation: If you already know the series ID you want (say by searching on the FRED website), you can fetch data easily into a pandas Series
End of explanation
info = fred.get_series_info('PAYEMS')
info['title']
Explanation: You can also easily fetch the meta data about any series
End of explanation
personal_income_series = fred.search_by_release(175, limit=5, order_by='popularity', sort_order='desc')
personal_income_series['title']
df = {}
df['SF'] = fred.get_series('PCPI06075')
df['NY'] = fred.get_series('PCPI36061')
df['DC'] = fred.get_series('PCPI11001')
df = pd.DataFrame(df)
df.plot()
Explanation: You can also get a set of series IDs programmatically by release or category IDs. Several sorting options are also available. On the FRED website I know that the release ID 175 contains some personal income data. Let's fetch 5 most popular series in that set.
End of explanation
df = fred.search_by_category(101, limit=10, order_by='popularity', sort_order='desc')
df['title']
Explanation: You can also search by categories. On the FRED website I see that category 101 contains data about Consumer Credit.
End of explanation
df = fred.search_by_release(151)
df['title'].head(10)
Explanation: As a example let's fetch the personal income data. Release 151 looks quite intersting
End of explanation
state_df = df[~df['title'].str.startswith('Per Capita Personal Income in the')]
len(state_df)
state_df.id.str[:2]
Explanation: I noticed that the data is mostly organized by state, except for a few that are by BEA region. We can use pandas to easily select the seires we want
End of explanation
income_by_state = {}
for series_id in state_df.index:
    income_by_state[series_id[:2]] = fred.get_series(series_id)
income_by_state = pd.DataFrame(income_by_state)
income_by_state.ix[-1].plot(kind='bar')
plt.title('Per Capita Personal Income by State')
Explanation: looks good, we got the data series for all 50 states here
End of explanation
<END_TASK>