Reputation: 23
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
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