Python write value from dictionary based on match in range of columns

2024/10/13 23:19:35

From my df showing employees with multiple levels of managers (see prior question here), I want to map rows to a department ID, based on a manager ID that may appear across multiple columns:

eid,    mid,    l2mid   l3mid
111,    112,    116,    115
113,    114,    115,    0
112,    117,    114,    0   
110,    115,    0,      0    
116,    118,    0,      0 

[edit: corrected data set to reflect row for eid=110 will be dropped edit #2: modified row for eid=112 to reflect that i need to search multiple columns to get a match in dictionary.]

The dictionary is

  country = {112: 'US', 114: 'Ireland', 118: 'Mexico'}

I'd like write Python that searches the manager columns 'mid':'l3mid' and then writes the country string value into the new column. I'd like to drop rows if they do not have a manager from one of the country codes in the dictionary. So the output I'm looking for is:

eid,    mid,    l2mid   l3mid   country
111,    112,    116,    115,    US
113,    114,    115,    0,      Ireland
112,    117,    114,    0       Ireland
116,    118,    0,      0       Mexico

I have tried building a function to do this but can't quite figure out the syntax. I appreciate your help as I'm new to this work.

Answer

I added a solution, if manager columns (mid,l2mid,l3mid) value match the dictionary keys, then the values are joined splitted by ,:

s = df.drop('eid',1).applymap(country.get).dropna(how='all', axis=0).apply(lambda x: ', '.join(x.dropna()), 1)df = df.loc[s.index].assign(country=s)
print (df)eid  mid  l2mid  l3mid          country
0  111  112    114    115          US, Ireland
1  113  114    115      0          Ireland
2  112  114    118      0          Ireland
4  116  118      0      0          Mexico
https://en.xdnf.cn/q/118024.html

Related Q&A

Merge two dataframes based on a column

I want to compare name column in two dataframes df1 and df2 , output the matching rows from dataframe df1 and store the result in new dataframe df3. How do i do this in Pandas ? df1place name qty unit…

Python/Pandas - building a new column based in columns comparison

I have this dataframe:df:CNPJ Revenues 2016 Revenues 2015 Revenues 2014 0 01.637.895/0001-32 R$ 12.696.658 NaN R$ 10.848.213 1 02.916.265/0001-60 …

Present blank screen, wait for key press -- how?

lo,I am currently trying to code a simple routine for an experiment we are planning to run. The experiment starts by entering a subject number and creating a bunch of files. I got that part working. Ne…

Images appearing in all but 1 flask page

I am creating a web app in flask, python, mysql. When viewing every other page on my website my images load, but when viewing one specific page, I cant get any images to load using already working code…

Python: Write nested list objects to csv file

Im trying to write data from a list of lists to a csv file. This is a simplified version of what I haveclass Point(object): def __init__(self, weight, height):self.weight = weightself.height = heightde…

Entire module is not detected by __init__.py

I have a relatively small python program which is setup like thisRoot--Network--DTOLots of py files which contain classes.Other py files in the projectBoth in the Network and the DTO folder there is an…

Python output to terminal during ssh login

I have been looking everywhere for this and have not found a solution. I am using python 2.5.1 on an Apple iPod and I am trying to connect to a host with SSH. First I start off with import os. Next I o…

Unable to find SIFT or xfeatures2d in OpenCV Python [duplicate]

This question already has answers here:PyCharm: Installation of non-free OpenCV modules for operations like SIFT, SURF(2 answers)Closed 6 years ago.I recently switch back to python for facial detection…

Django paginate for django 2

I need to use pagination to a Django list but I couldnt find any help online,, only old docs from Django version 1.3 here are my files : views.pydef home(request):all_dress = Item.objects.all().filter(…

AttributeError Button object has no attribute scrlFBtn

from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.label import Label from kivy.core.window import Window from kivy.uix.scrollview import ScrollView from kivy.effects.scrol…