0

How to use regex in pandas to extract below field. the below is one of my pandas dataframe column value, but i wanted to only extract 'eastus' and keep it as value for this field. how to filter this. this position is always fixed

Sample df:

                          correlationId                                                 id                                                                level   ...      status.value status.localizedValue            tag
0    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  /subscriptions/xxxxxxxxxxxxxxxxxxxxx/resourcegroups/xxxxxxxxxxxx/providers/Microsoft.RecoveryServices/locations/eastus/events/xxxxxxxxxxxx/ticks/xxxxxxxx  Informational  ...    Succeeded             Succeeded  Managed by IT

command i tried:

if not df.empty:
        columns = ["correlationId","eventName.value","id","resourceGroupName","resourceProviderName.value","operationName.value","status.value","eventTimestamp","submissionTimestamp"]        
        df.columns = df.columns.to_series().apply(lambda x: x.strip())
        #print(df.columns)    
        df.fillna('Missing', inplace=True)
        drop_these = ['correlationId']
        df['Location'] = df.id.str.split("/")[8]

but its not working

Error:

 df['Location'] = df.id.split("/")[8]
  File "C:\Python37\lib\site-packages\pandas\core\generic.py", line 5274, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'split'

any suggestion please

3
  • 1
    please share a sample of the dataframe, you are trying to set one value to the whole dataframe, is that what you are trying to achieve ? also what do you mean by not working ? what is happening exactly ? stackoverflow.com/help/how-to-ask Commented Aug 13, 2020 at 10:32
  • 1
    As it is, it should work. So maybe is a problem with your dataframe Commented Aug 13, 2020 at 10:34
  • shall updates with some sample data shortly my apologies Commented Aug 13, 2020 at 10:41

1 Answer 1

1
id = '/subscriptions/xxxxxxxx/resourcegroups/xxxxxxxx/providers/Microsoft.RecoveryServices/' \
     'locations/eastus/events/xxxxxxx/ticks/xxxxx'
df = pd.DataFrame({
    'sample':[id]
})
df['Location'] = df['sample'].str.split("/",expand=True)[8]

print(df)
    sample                                                            Location
0  /subscriptions/xxxxxxxx/resourcegroups/xxxxxxxx/providers/Microsoft.RecoveryServices/locations/eastus/events/xxxxxxx/ticks/xxxxx   eastus
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.