0

Update: Now I am getting the below error: AttributeError: 'list' object has no attribute 'str' , I think the original error was due to the fact that I needed 2 square brackets in the code for the Dataframe to be recognized as a Dataframe rather than a series. now I have this

AttributeError: 'list' object has no attribute 'str'

Updated Code

## Create list of Privileged accounts
Search_for_These_values = ['Privileged','Diagnostics','SYS','service account'] #creating list

pattern = '|'.join(Search_for_These_values)     # joining list for comparision

PrivilegedAccounts_DF['PrivilegedAccess'] = PrivilegedAccounts_DF.columns=[['first_name']].str.contains(pattern)
PrivilegedAccounts_DF['PrivilegedAccess'] = PrivilegedAccounts_DF['PrivilegedAccess'].map({True: 'True', False: 'False'})

ORIGINAL Question:

I'm just wondering how one might overcome the below error.

TypeError: only integer scalar arrays can be converted to a scalar index

I am using the below code on a pandas dataframe (126 rows × 6 columns) What I am trying to do is create a new column "PrivilegedAccess" and in this column I want to write "True" if any of the names in the first_names column match the ones outlined in the "Search_for_These_values" list and "False" if they don't

SAMPLE DATA:

    uid last_name   first_name  language    role    email_address   department
0   121 Chad    Diagnostics English Team Lead   Michael.chad@gmail.com  Data Scientist
1   253 Montegu Paulo   Spanish CIO Paulo.Montegu@gmail.com Marketing
2   545 Mitchel Susan   English Team Lead   Susan.Mitchel@gmail.com Data Scientist
3   555 Vuvko   Matia   Polish  Marketing Lead  Matia.Vuvko@gmail.com   Marketing
4   568 Sisk    Ivan    English Supply Chain Lead   Ivan.Sisk@gmail.com Supply Chain
5   475 Andrea  Patrice Spanish Sales Graduate  Patrice.Andrea@gmail.com    Sales
6   365 Akkinapalli Cherifa French  Supply Chain Assistance Cherifa.Akkinapalli@gmail.com   Supply Chain

CODE:

## Create list of Privileged accounts
Search_for_These_values = ['Privileged','Diagnostics','SYS','service account'] #creating list

pattern = '|'.join(Search_for_These_values)     # joining list for comparision

PrivilegedAccounts_DF['PrivilegedAccess'] = PrivilegedAccounts_DF['first_name'].str.contains(pattern)
PrivilegedAccounts_DF['PrivilegedAccess'] = PrivilegedAccounts_DF['PrivilegedAccess'].map({True: 'Yes', False: 'No'})

Many thanks

sqlworrier
  • 53
  • 5
  • Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) (MRE). We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. This lets us test our suggestions against your test data and desired output. Please [include a minimal data frame](https://stackoverflow.com/questions/52413246/how-to-provide-a-reproducible-copy-of-your-dataframe-with-to-clipboard) as part of your MRE. – Prune Mar 29 '21 at 21:03
  • Hi @Prune, thank you for you comments. I have now included a reduced version of the dataframe and some code that you are able to copy. – sqlworrier Mar 30 '21 at 06:15
  • Unfortunately the sample doesn't reproduce the error. Your code works fine as shown, and for example if I add `Michael` and `Susan` to the search list, they get `Yes` for `PrivilegedAccess`. – tdy Mar 30 '21 at 06:27
  • Okay so it looks like I needed 2 square brackets in the code for the Dataframe to be recognized as a Dataframe rather than a series. now I have this Error: AttributeError: 'DataFrame' object has no attribute 'str – sqlworrier Mar 30 '21 at 07:41
  • The dtype of the column I am trying to reference is 'object', I am still trying to resolve the issue – sqlworrier Mar 30 '21 at 07:52
  • When we have an MRE, we can work on your problem. – Prune Mar 30 '21 at 16:23

0 Answers0