Dayo Chan
Dayo Chan

Reputation: 21

Map list of elements to category of element in pandas?

I have been googling a lot, but I still can't find a quick way to do so. Say I have a column in my csv file:

1. C.Ronald         
2. Conor McGregor    
3. Lionel Messi
4. LeBron James
5. Derrick Rose   
6. Tom Brady
7. ...
8. ...

and so on, I want to replace those name as the following three categories through python:

1. Soccer player
2. MMA fighter
3. Soccer player
4. NBA player
5. NBA player
6. NFL plaer
7. ... 
8. ...

How can I replace 'C.Ronald','Lionel Messi' and those soccer player's name, in to a class at once, but not one by one, since I have a long column.

Upvotes: 2

Views: 445

Answers (1)

jpp
jpp

Reputation: 164613

You need to create a mapping form type of player to list of players.

You can then use a data-oriented package such as pandas to perform the mapping for you via a dictionary:

from io import StringIO
import pandas as pd

mystr = StringIO("""C.Ronald
Conor McGregor
Lionel Messi
LeBron James
Derrick Rose
Tom Brady
""")

df = pd.read_csv(mystr, header=None, names=['Player'])

d = {'Soccer player': ['C.Ronald', 'Lionel Messi'],
     'MMA fighter': ['Conor McGregor'],
     'NBA player': ['LeBron James', 'Derrick Rose'],
     'NFL player': ['Tom Brady']}

df['Category'] = df['Player'].map(lambda x: next((k for k, v in d.items() if x in v), None))

#            Player       Category
# 0        C.Ronald  Soccer player
# 1  Conor McGregor    MMA fighter
# 2    Lionel Messi  Soccer player
# 3    LeBron James     NBA player
# 4    Derrick Rose     NBA player
# 5       Tom Brady     NFL player

Explanation

next on a generator expression returns the next iteration; it stops when it finds the first instance. This will only become an issue if you had a sportsperson in more than one category. The argument None only gets returned if no match is found.

Upvotes: 2

Related Questions