This is my pandas data-frame. I want to modify values of first column (System) by extracting just a,b,c,d. How can this be done in python
System mem
/vol/a/ 10
/vol/b/ 20
/vol/c/ 30
/vol/d/ 40
This is my pandas data-frame. I want to modify values of first column (System) by extracting just a,b,c,d. How can this be done in python
System mem
/vol/a/ 10
/vol/b/ 20
/vol/c/ 30
/vol/d/ 40
You can use .str.extract:
In [11]: df.System.str.extract("/vol/(.*?)/", expand=False)
Out[11]:
0 a
1 b
2 c
3 d
Name: System, dtype: object
"/vol/(.*?)/.*"? is not required here./vol/a/.hidden/.git, see OP's comment above. A bit unclear what he really wants.Can be done in multiple ways, here is one
df['System'] = df['System'].str.split('/').str[-2]
System mem
0 a 10
1 b 20
2 c 30
3 d 40
Option 2:
df['System'] = df.System.str.replace('[/vol/|/]', '')
Andy Hayden already covered str.extract
Using str.extract:
import pandas as pd
df = pd.DataFrame({'System': ['/vol/a/', '/vol/b/', '/vol/c/', '/vol/d/'], 'mem': [10, 20, 30, 40]})
df['new_column'] = df['System'].str.extract(r'([^/]+)/?$')
print(df)
This yields
System mem new_column
0 /vol/a/ 10 a
1 /vol/b/ 20 b
2 /vol/c/ 30 c
3 /vol/d/ 40 d