1

II have a serie object containing 3 columns (name, code, value) which I get from the below function:

def get_fuzz(df, w):
    s = df['Legal_Name'].apply(lambda y: fuzz.token_set_ratio(y, w))
    idx = s.idxmax()
    return {'name': df['Legal_Name'].iloc[idx], 'lei': df['LEI'].iloc[idx], 'val': s.max()}

df1['Name'].apply(lambda x: get_fuzz(df, x))

The Serie looks like this

output
0    {'name': 'MGR Farms LLC', 'lei': '984500486BBD...
1    {'name': 'RAVENOL NORGE AS', 'lei': '549300D2O...
2    {'name': 'VCC Live Group Zártkörűen Működő Rés...

I can assign the output to my dataframe with the code below.

df1.assign(search=df1['Name'].apply(lambda x: get_fuzz(df, x)))

The Dataframe that I get looks like this

    ID  Name             search
0   1   Marshalll   {'name': 'MGR Farms LLC', 'lei': '984500486BBD...
1   2   JP Morgan   {'name': 'RAVENOL NORGE AS', 'lei': '549300D2O...

Question

How can I split this column into 3 columns?

Final output wanted

    ID  Name        Name_bis            LEI             Value
0   1   Marshalll   MGR Farms LLC    984500486BBD        57
1   2   Zion        ZION INVESTMENT  549300D2O           100
CDJB
  • 14,043
  • 5
  • 29
  • 55
A2N15
  • 595
  • 4
  • 20

1 Answers1

1

Assuming you have the dataframe set up as:-

>>> df
   ID       Name                                             search
0   1  Marshalll  {'name': 'MGR Farms LLC', 'lei': '984500486BBD...
1   2  JP Morgan  {'name': 'RAVENOL NORGE AS', 'lei': '549300D2O...

you can use:-

>>> df = pd.concat([df.drop(['search'], axis=1), df['search'].apply(pd.Series)], axis=1)
>>> df
   ID       Name              name           lei  value
0   1  Marshalll     MGR Farms LLC  984500486BBD     57
1   2  JP Morgan  RAVENOL NORGE AS     549300D2O    100

And then update the column names as needed:-

>>> df.columns = ['ID', 'Name', 'Name_bis', 'LEI', 'Value']
>>> df
    ID       Name          Name_bis           LEI  Value
0   1  Marshalll     MGR Farms LLC  984500486BBD     57
1   2  JP Morgan  RAVENOL NORGE AS     549300D2O    100
CDJB
  • 14,043
  • 5
  • 29
  • 55