i have got 2 Dataframes that look like this: df=pd.DataFrame({'PERSONALNUMMER': {4756: '0209740',4820: '0234212',4855: '0251297',4750: '0209326',4992: '4000404'}, 'MANDANT': {4756: 'OM', 4820: 'OM', 4855: 'OM', 4750: 'OM', 4992: 'OM'}, 'Fachabteilung': {4756: 'HA2300',4820: 'HA2300',4855: 'HA2300',4750: 'HA2300',4992: 'HA2300'}, 'FACHEXPERTISE': {4756: 'AQ10',4820: 'AQ10',4855: 'AQ10',4750: 'AQ10',4992: 'AQ10'}, 'Leistungsgruppe': {4756: pd.NA,4820: 'Endoprothetik Knie',4855: 'Allgemeine Chirurgie',4750: 'Wirbelsaeuleneingriffe',4992: pd.NA}}) MAP=pd.DataFrame({'MANDANT': {238: 'OM', 239: 'OM', 240: 'OM', 241: 'OM'}, 'Fachabteilung': {238: 'HA2300', 239: 'HA2300', 240: 'HA2300', 241: 'HA2300'}, 'FACHEXPERTISE': {238: 'AQ10', 239: 'AQ10', 240: 'AQ10', 241: 'AQ10'}, 'Leistungsgruppe': {238: 'Allgemeine Chirurgie', 239: 'Endoprothetik Huefte',240: 'Endoprothetik Knie',241: 'Revision Huefte'}, 'VK': {238: 3,239: 14,240: 28,241: 22}}) i want to do the following: I got empty entries in my df Dataframe in column "Leistungsgruppe" and i need to pick RANDOM elements out of my MAP Dataframe based on conditions. so i want to iterate over my df and pick element(s) The Conditions are like: the MANDANT, Fachabteilung and FACHEXPERTISE need to be the same and if there are more than 3 entries for that element in my MAP Dataframe, i need to narrow it down to 3 elements. right now i have no clue how to do this.. because my Python knowledge when it comes to iterating on single rows is almost non existant.. i tried to think of a way to do this with iterrows but i can not come up with a quick solution right now. Continue reading...