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:
JavaScript
x
3
1
correlationId id level status.value status.localizedValue tag
2
0 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx /subscriptions/xxxxxxxxxxxxxxxxxxxxx/resourcegroups/xxxxxxxxxxxx/providers/Microsoft.RecoveryServices/locations/eastus/events/xxxxxxxxxxxx/ticks/xxxxxxxx Informational Succeeded Succeeded Managed by IT
3
command i tried:
JavaScript
1
8
1
if not df.empty:
2
columns = ["correlationId","eventName.value","id","resourceGroupName","resourceProviderName.value","operationName.value","status.value","eventTimestamp","submissionTimestamp"]
3
df.columns = df.columns.to_series().apply(lambda x: x.strip())
4
#print(df.columns)
5
df.fillna('Missing', inplace=True)
6
drop_these = ['correlationId']
7
df['Location'] = df.id.str.split("/")[8]
8
but its not working
Error:
JavaScript
1
5
1
df['Location'] = df.id.split("/")[8]
2
File "C:Python37libsite-packagespandascoregeneric.py", line 5274, in __getattr__
3
return object.__getattribute__(self, name)
4
AttributeError: 'Series' object has no attribute 'split'
5
any suggestion please
Advertisement
Answer
JavaScript
1
11
11
1
id = '/subscriptions/xxxxxxxx/resourcegroups/xxxxxxxx/providers/Microsoft.RecoveryServices/'
2
'locations/eastus/events/xxxxxxx/ticks/xxxxx'
3
df = pd.DataFrame({
4
'sample':[id]
5
})
6
df['Location'] = df['sample'].str.split("/",expand=True)[8]
7
8
print(df)
9
sample Location
10
0 /subscriptions/xxxxxxxx/resourcegroups/xxxxxxxx/providers/Microsoft.RecoveryServices/locations/eastus/events/xxxxxxx/ticks/xxxxx eastus
11