John Schank
John Schank

Reputation: 61

Exception has occurred: TypeError 'in <string>' requires string as left operand, not Series

I am working with Pandas in python I have 2 Dataframes that I am trying to pull infromation from one to the other. Here is a example of the code.

    import pandas as pd

employees = {'Names': ['John','Jack','Frank','Mike'],
             'Employee_id': ['123', '124', '125', '126'],
             'Leader': ['','','','']}

df1 = pd.DataFrame(employees, columns = ['Names', 'Employee_id', 'Leader'])

print(df1)

leader = {'Employee_id': ['123', '124', '125', '126'],
          'Supervisor': ['Nick', 'Jeff', 'Nick', 'Jeff']}

df2 = pd.DataFrame(leader, columns = ['Employee_id', 'Supervisor'])
print(df2)

df1['Leader'] = df1['Employee_id'].map(lambda x: df2['Supervisor'] if df2['Employee_id'] in x else "")

print(df1)

Here is the error.

Exception has occurred: TypeError
'in <string>' requires string as left operand, not Series

Not sure what I am doing wrong.

Thank you for your help.

Upvotes: 1

Views: 1353

Answers (2)

Quang Hoang
Quang Hoang

Reputation: 150735

In your code df2['Employee_id'] in x, x is a string and df2['Employee_id'] is a pandas series. Python doesn't know how to check for series in big_string operation. More specifically, it can only check for some_string in big_string.

IN your case, you can use map with a series:

df1['Leader'] = df1['Employee_id'].map(df2.set_index('Employee_id')['Supervisor'])

Output:

   Names Employee_id Leader
0   John         123   Nick
1   Jack         124   Jeff
2  Frank         125   Nick
3   Mike         126   Jeff

Upvotes: 1

Mayank Porwal
Mayank Porwal

Reputation: 34046

Use df.merge:

In [79]: df = df1.merge(df2, on='Employee_id') 

In [80]: df
Out[80]: 
   Names Employee_id Leader Supervisor
0   John         123              Nick
1   Jack         124              Jeff
2  Frank         125              Nick
3   Mike         126              Jeff

Upvotes: 0

Related Questions