vol_of_vol
vol_of_vol

Reputation: 23

Add a new column to pandas data frame from dictionary

I want to add a new column to my dataframe and map it to a dictionary. Mapping should be on the index of my original dataframe and I don't know how to use .map() to get there.

d={'KO': 'Consumer, Non-cyclical', 'AAPL': 'Technology'}

df:

Date   2015-12-01   
KO    2144.499950  
AAPL  5162.959824  

I would like the result too look like this:

df:

Date   2015-12-01  industry 
KO    2144.499950  Consumer, Non-cyclical
AAPL  5162.959824  Technology

Upvotes: 2

Views: 5108

Answers (1)

Andy Hayden
Andy Hayden

Reputation: 375905

Construct the Series first:

df["industry"] = pd.Series(d)

Note: This assumes that the dict keys are in the DataFrame index:

In [11]: df
Out[11]:
       2015-12-01
KO    2144.499950
AAPL  5162.959824

In [12]: df["industry"] = pd.Series(d)

In [13]: df
Out[13]:
       2015-12-01                industry
KO    2144.499950  Consumer, Non-cyclical
AAPL  5162.959824              Technology

Upvotes: 4

Related Questions