ria
ria

Reputation: 31

modify a column in pandas data-frame selecting a sub-string

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

Upvotes: 1

Views: 159

Answers (4)

piRSquared
piRSquared

Reputation: 294488

By using rsplit you can have as much stuff in front as you'd like.

df.assign(New=df.System.str.rsplit('/', 2).str[-2])

    System  mem New
0  /vol/a/   10   a
1  /vol/b/   20   b
2  /vol/c/   30   c
3  /vol/d/   40   d

Upvotes: 1

Jan
Jan

Reputation: 43169

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

Upvotes: 3

Vaishali
Vaishali

Reputation: 38415

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

Upvotes: 4

Andy Hayden
Andy Hayden

Reputation: 375685

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

Upvotes: 4

Related Questions