Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map key to multiple values to dataframe column?

I have a df column that looks like this:

col1
Non Profit
Other-501c3
501c3
Sole Proprietor

How can I create a dictionary object or mapping layer(open to all suggestions) where I can pass any value if it matches criteria and changes to the key value?

For example if the value is Other-501c3 then change it to non-profit.

Examples (everything after the equal sign needs to change to the value before equal sign):

1. non-profit = (Non Profit, Other-501c3, 501c3,NON-Profit, Not-for-profit).

2. Sole Proprietor = (Sole Proprietor,Sole Proprietorship)

The solution should be scalable I can add in more 'key value' pairs

Thank you in advance.

like image 662
RustyShackleford Avatar asked Oct 11 '18 13:10

RustyShackleford


People also ask

Can we use map function on DataFrame?

In this article, I have explained map() function is from the Series which is used to substitute each value in a Series with another value and returns a Series object, since DataFrame is a collection of Series, you can use the map() function to update the DataFrame.

How do I select multiple columns using LOC?

Using df[] & loc[] to Select Multiple Columns by Name DataFrame. loc[] you can select multiple columns by names or labels. To select the columns by names, the syntax is df.


1 Answers

Create dictionaries from keys, merge them and map:

L1 = ['Non Profit', 'Other-501c3', '501c3','NON-Profit', 'Not-for-profit']
d1 = dict.fromkeys(L1, 'non-profit')

L2 = ['Sole Proprietor','Sole Proprietorship']
d2 = dict.fromkeys(L2, 'Sole Proprietor')

d = {**d1, **d2}
print (d)
{'Non Profit': 'non-profit', 
 'Other-501c3': 'non-profit', 
 '501c3': 'non-profit',
 'NON-Profit': 'non-profit', 
 'Not-for-profit': 'non-profit', 
 'Sole Proprietor': 'Sole Proprietor',
 'Sole Proprietorship': 'Sole Proprietor'}

df['new'] = df['col1'].map(d)
print (df)
              col1              new
0       Non Profit       non-profit
1      Other-501c3       non-profit
2            501c3       non-profit
3  Sole Proprietor  Sole Proprietor
like image 145
jezrael Avatar answered Sep 17 '22 22:09

jezrael