I've imported a spreadsheet I'd exported from Linkedin of my connections and want to categorize people's positions at different levels.
So, I've created a dictionary with the terms to look up each position level.
A first version of the dictionary would be:
dicpositions = {'0 - CEO, Founder': ['CEO', 'Founder', 'Co-Founder', 'Cofounder', 'Owner'],
'1 - Director of': ['Director', 'Head'],
'2 - Manager': ['Manager', 'Administrador'],
'3 - Engenheiro': ['Engenheiro', 'Engineering'],
'4 - Consultor': ['Consultor', 'Consultant'],
'5 - Estagiário': ['Estagiário', 'Intern'],
'6 - Desempregado': ['Self-Employed', 'Autônomo'],
'7 - Professor': ['Professor', 'Researcher'] }
And I need a code to read each position in my spreadsheet, check if there is any of these terms and return the equivalent key in another specific column.
A sample data of the dataframe i'm reading would be:
sample = pd.Series(data = (['(blank)'], ['Estagiário'], ['Professor', 'Adjunto'],
['CEO', 'and', 'Founder'], ['Engenheiro', 'de', 'Produção'],
['Consultant'], ['Founder', 'and', 'CTO'],
['Intern'], ['Manager', 'Specialist'],
['Administrador', 'de', 'Novos', 'Negócios'],
['Administrador', 'de', 'Serviços']))
Which Returns:
0 [(blank)]
1 [Estagiário]
2 [Professor, Adjunto]
3 [CEO, and, Founder]
4 [Engenheiro, de, Produção]
5 [Consultant]
6 [Founder, and, CTO]
7 [Intern]
8 [Manager, Specialist]
9 [Administrador, de, Novos, Negócios]
10 [Administrador, de, Serviços]
dtype: object
I've done the following code:
import pandas as pd
plan = pd.read_excel('SpreadSheet Name.xlsx', sheet_name = 'Positions')
list0 = ['CEO', 'Founder', 'Co-Founder', 'Cofounder', 'Owner']
list1 = ['Director', 'Head']
list2 = ['Manager', 'Administrador']
listgeral = [dic0, dic1, dic2]
def in_list(list_to_search,terms_to_search):
results = [item for item in list_to_search if item in terms_to_search]
if len(results) > 0:
return '0 - CEO, Founder'
else:
pass
plan['PositionLevel'] = plan['Position'].str.split().apply(lambda x: in_list(x, listgeral[0]))
Actual output:
Position PositionLevel
0 '(blank)' None
1 'Estagiário' None
2 'Professor Adjunto' None
3 'CEO and Founder' '0 - CEO, Founder'
4 'Engenheiro de produção' None
5 'Consultant' None
6 'Founder and CTO' '0 - CEO, Founder'
7 'Intern' None
8 'Manager Specialist' None
9 'Administrador de Novos Negócios' None
Expected output:
Position PositionLevel
0 '(blank)' None
1 'Estagiário' '5 - Estagiário'
2 'Professor Adjunto' '7 - Professor'
3 'CEO and Founder' '0 - CEO, Founder'
4 'Engenheiro de produção' '3 - Engenheiro'
5 'Consultant' '4 - Consultor'
6 'Founder and CTO' '0 - CEO, Founder'
7 'Intern' '5 - Estagiário'
8 'Manager Specialist' '2 - Manager'
9 'Administrador de Novos Negócios' '2 - Manager'
First I was planning to run that code for every list inside my listgeral
, but I coundn't do so. Then I started to believe it would be better to apply this one for a big dictionary, just as the dicpositions
from the beginning of the question, and the return the key of the term.
I've tried to apply the following code to this program:
dictest = {'0 - CEO, Founder': ['CEO', 'Founder', 'Co-Founder', 'Cofounder', 'Owner'],
'1 - Director of': ['Director', 'Head'],
'2 - Manager': ['Manager', 'Administrador']}
def in_dic (x, dictest):
for key in dictest:
for elem in dictest[key]:
if elem == x:
return key
return False
Where the output from in_dic('CEO', dictest)
Is '0 - CEO, Founder'
And, for example, the output from in_dic('Banana', dictest)
is False
But I couldn't advance from it and apply this function in_dic()
to my problem.
I would really appreciate anybody's help.
Thanks a lot.