TypeError: string indices must be integers with Pandas apply and lambda

Question:

I have below data frame.

dictA = {'Event_ID': {0: 'G-00001',   1: 'G-00002 ',   2: 'G-00003',   3: 'G-00004',   4: 'G-00005',   5: 'G-00006',   6: 'G-00007',   7: 'G-00008',   8: 'G-00009',   9: 'G-00010',   10: 'G-00011',   11: 'G-00012',   12: 'G-00013',   13: 'G-00014',   14: 'G-00015',   15: 'G-00016',   16: 'G-00017'},
 'Name': {0: 'ABC',   1: 'CSA',   2: 'CSA',   3: 'VSX',   4: 'ABC',   5: 'ABC',   6: 'CSA',   7: 'ABC',   8: 'VSX',   9: 'CSA',   10: 'VSX',   11: 'ABC',   12: 'VSX',   13: 'VSX',   14: 'ABC',   15: 'ABC',   16: 'CSA'},
 'CompanyAd      ': {0: '51Job, Inc.Cayman Islands NMS              ',   1: "724 Solution's Inc. Canada NMS             ",   2: 'A B SKF Sweden OTC+              ',   3: 'A/S Steamship Company Torm Denmark"s NMS OTC+       ',   4: 'ABB Ltd. Switzerland OTC+             ',   5: 'Aber Diamond Ltd. Canada CAP MKT                    ',   6: 'Abitibi Consolidated Inc. Canada OTC +              ',   7: 'ABN Amro Bank N.V. Netherlands AMEX - Preferred OTC+',   8: 'ABN Amro Holding N.V. Netherlands NYSE              ',   9: 'Acambis plc United Kingdom OTC +              ',   10: "Ace Aviation Holdings'aed Inc. Canada OTC           ",   11: 'Acetex Corp. Canada OTC - Debt+              ',   12: 'Acrex Ventures, Ltd. Canada OTC+             ',   13: 'ACS-Tech 80 Ltd. Israel CAP MKT              ',   14: 'Actions Semiconductor Co. Ltd. Cayman Islands NMS   ',   15: 'Adastra Minerals Inc. Canada OTC*              ',   16: 'ADB Systems International Inc. Canada OTC           '},
 'ticket': {0: 671,   1: 5,   2: 5,   3: 23,   4: 4,   5: 60,   6: 60,   7: 89,   8: 0,   9: 6,   10: 3,   11: 2,   12: 4,   13: 32,   14: 3,   15: 1,   16: 23},
 'Revenue': {0: 6720,   1: 56,   2: 78,   3: 34,   4: 89,   5: 73,   6: 345,   7: 890,   8: 0,   9: 45,   10: 39,   11: 34,   12: 89,   13: 127,   14: 84,   15: 100,   16: 525},
 'Expences': {0: 150.0,   1: 18.0,   2: 38.0,   3: 23.0,   4: 150.0,   5: 55.0,   6: 110.0,   7: 150.0,   8: 0.0,   9: 16.0,   10: 23.0,   11: 150.0,   12: 48.0,   13: 35.0,   14: 55.0,   15: 150.0,   16: nan},
 'expect': {0: 50.0,   1: 100.0,   2: 100.0,   3: nan,   4: 40.0,   5: 60.0,   6: 60.0,   7: nan,   8: 50.0,   9: 60.0,   10: 30.0,   11: 20.0,   12: 40.0,   13: 10.0,   14: 120.0,   15: 140.0,   16: 90.0},
 'Signed_Date': {0: Timestamp('2021-06-01 00:00:00'),   1: Timestamp('2021-06-05 00:00:00'),   2: Timestamp('2021-06-03 00:00:00'),   3: Timestamp('2021-06-03 00:00:00'),   4: Timestamp('2021-06-02 00:00:00'),   5: Timestamp('2021-04-15 00:00:00'),   6: Timestamp('2021-06-12 00:00:00'),   7: Timestamp('2021-06-02 00:00:00'),   8: Timestamp('2021-04-30 00:00:00'),   9: Timestamp('2021-06-22 00:00:00'),   10: Timestamp('2021-06-10 00:00:00'),   11: Timestamp('2021-06-03 00:00:00'),   12: Timestamp('2021-06-12 00:00:00'),   13: Timestamp('2021-04-24 00:00:00'),   14: Timestamp('2021-04-21 00:00:00'),   15: Timestamp('2021-06-07 00:00:00'),   16: Timestamp('2021-04-02 00:00:00')}}
df = pd.DataFrame.from_dict(dictA)


    Event_ID Name    CompanyAd                                             ticket   Revenue Expences    expect   Signed_Date    
0   G-00001  ABC     51Job, Inc.Cayman Islands NMS                         671      6720    150         50       01 June 2021   
1   G-00002  CSA     724 Solution's Inc. Canada NMS                         5       56      18          100      05 June 2021   
2   G-00003  CSA     A B SKF Sweden OTC+                                    5       78      38          100      03 June 2021   
3   G-00004  VSX     A/S Steamship Company Torm Denmark"s NMS OTC+         23       34      23          NaN      03 June 2021   
4   G-00005  ABC     ABB Ltd. Switzerland OTC+                             4        89      150         40       02 June 2021   
5   G-00006  ABC     Aber Diamond Ltd. Canada CAP MKT                      60       73      55          60       15 April 2021  
6   G-00007  CSA     Abitibi Consolidated Inc. Canada OTC +                60       345     110         60       12 June 2021   
7   G-00008  ABC     ABN Amro Bank N.V. Netherlands AMEX - Preferred OTC+  89       890     150         NaN      02 June 2021   
8   G-00009  VSX     ABN Amro Holding N.V. Netherlands NYSE                 0       0       0           50       30 April 2021  
9   G-00010  CSA     Acambis plc United Kingdom OTC +                       6       45      16          60       22 June 2021   
10  G-00011  VSX     Ace Aviation Holdings'aed Inc. Canada OTC              3       39      23          30       10 June 2021   
11  G-00012  ABC     Acetex Corp. Canada OTC - Debt+                        2       34      150         20       03 June 2021   
12  G-00013  VSX     Acrex Ventures, Ltd. Canada OTC+                       4       89      48          40       12 June 2021   
13  G-00014  VSX     ACS-Tech 80 Ltd. Israel CAP MKT                        32      127     35          10       24 April 2021  
14  G-00015  ABC     Actions Semiconductor Co. Ltd. Cayman Islands NMS      3       84      55          120      21 April 2021  
15  G-00016  ABC     Adastra Minerals Inc. Canada OTC*                      1       100     150         140      7 June 2021    
16  G-00017  CSA     ADB Systems International Inc. Canada OTC              23      525                 90       02 April 2021

I want to change the "Name" column value to "ASD" if the "CompanyAd" column contained "OTC+" or "OTC +". I’m using the below code and I’m getting an error, "TypeError: string indices must be integers"

df['account_name_e']=df['CompanyAd      '].apply(str).str.replace(" ","")
df['account_name_e'] = df['account_name_e'].apply(str).str.replace("(?i)[^0-9a-z+]",'')
df['NameE'] = df['Name']
df['NameE'] = df['NameE'].apply(lambda row: str('ASD') if 'OTC+' in str(row['account_name_e']) else row)

df

What should I want to do to resolve it?
Thanks in advance!

Asked By: rra

||

Answers:

>>> df.loc[df['CompanyAd      '].str.contains("OTC+|OTC +"), 'Name'] = "ASD"

>>> df
    Event_ID Name                                    CompanyAd        ticket  Revenue  Expences  expect Signed_Date
0    G-00001  ABC        51Job, Inc.Cayman Islands NMS                   671     6720     150.0    50.0  2021-06-01
1   G-00002   CSA        724 Solution's Inc. Canada NMS                    5       56      18.0   100.0  2021-06-05
2    G-00003  ASD                  A B SKF Sweden OTC+                     5       78      38.0   100.0  2021-06-03
3    G-00004  ASD  A/S Steamship Company Torm Denmark"s NMS OTC+ ...      23       34      23.0     NaN  2021-06-03
4    G-00005  ASD             ABB Ltd. Switzerland OTC+                    4       89     150.0    40.0  2021-06-02
5    G-00006  ABC  Aber Diamond Ltd. Canada CAP MKT              ...      60       73      55.0    60.0  2021-04-15
6    G-00007  ASD  Abitibi Consolidated Inc. Canada OTC +        ...      60      345     110.0    60.0  2021-06-12
7    G-00008  ASD  ABN Amro Bank N.V. Netherlands AMEX - Preferre...      89      890     150.0     NaN  2021-06-02
8    G-00009  VSX  ABN Amro Holding N.V. Netherlands NYSE        ...       0        0       0.0    50.0  2021-04-30
9    G-00010  ASD     Acambis plc United Kingdom OTC +                     6       45      16.0    60.0  2021-06-22
10   G-00011  ASD  Ace Aviation Holdings'aed Inc. Canada OTC     ...       3       39      23.0    30.0  2021-06-10
11   G-00012  ASD      Acetex Corp. Canada OTC - Debt+                     2       34     150.0    20.0  2021-06-03
12   G-00013  ASD      Acrex Ventures, Ltd. Canada OTC+                    4       89      48.0    40.0  2021-06-12
13   G-00014  VSX      ACS-Tech 80 Ltd. Israel CAP MKT                    32      127      35.0    10.0  2021-04-24
14   G-00015  ABC  Actions Semiconductor Co. Ltd. Cayman Islands ...       3       84      55.0   120.0  2021-04-21
15   G-00016  ASD    Adastra Minerals Inc. Canada OTC*                     1      100     150.0   140.0  2021-06-07
16   G-00017  ASD  ADB Systems International Inc. Canada OTC     ...      23      525       NaN    90.0  2021-04-02
Answered By: Amir saleem
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.