Shagun
Shagun

Reputation: 7

Get values from another a column based on a condition using loop

This is the dataframe I have I need to get the following:

 empid    role
   123    lead
   124    lead
   125 manager
   156    lead
   129 manager

while iterating over each value of a column role I need to get the value of empid if role == lead and store it in another variable

Upvotes: 0

Views: 592

Answers (1)

Corralien
Corralien

Reputation: 120409

Use .loc:

>>> df.loc[df['role'] == 'lead', 'empid']
0    123
1    124
3    156
Name: empid, dtype: int64

If you want a list:

empid = df.loc[df['role'] == 'lead', 'empid'].tolist()
>>> empid
[123, 124, 156]

Read the documentation Indexing and selecting data

Upvotes: 1

Related Questions