In [1]:
import pandas as pd
import numpy as np

pd.__version__, np.__version__


Out[1]:
('0.24.2', '1.16.3')

In [2]:
df = pd.DataFrame({
    'name': ['alice smith','bob jones','charlie joneson','daisy white'],
    'age': [25,20,30,35]
})

In [3]:
df


Out[3]:
name age
0 alice smith 25
1 bob jones 20
2 charlie joneson 30
3 daisy white 35

select rows by partial string


In [4]:
df[df['name'].str.contains('jones',regex=False)]


Out[4]:
name age
1 bob jones 20
2 charlie joneson 30

select rows by regular expression


In [5]:
df[df['name'].str.contains('^b|da')]


Out[5]:
name age
1 bob jones 20
3 daisy white 35

concatenate string columns


In [6]:
df = pd.DataFrame({
    'first_name': ['alice','bob','charlie','daisy'],
    'last_name':['smith','jones','joneson','white'],
    'age': [25,20,30,35]
})
df


Out[6]:
first_name last_name age
0 alice smith 25
1 bob jones 20
2 charlie joneson 30
3 daisy white 35

In [7]:
df['full_name'] = df['first_name'] + df['last_name']
df


Out[7]:
first_name last_name age full_name
0 alice smith 25 alicesmith
1 bob jones 20 bobjones
2 charlie joneson 30 charliejoneson
3 daisy white 35 daisywhite

In [8]:
df['full_name'] = df['first_name'] + ' ' + df['last_name']
df


Out[8]:
first_name last_name age full_name
0 alice smith 25 alice smith
1 bob jones 20 bob jones
2 charlie joneson 30 charlie joneson
3 daisy white 35 daisy white

split string column


In [9]:
df = pd.DataFrame({
    'name': ['alice smith','bob jones','charlie joneson','daisy white'],
    'age': [25,20,30,35]
})
df


Out[9]:
name age
0 alice smith 25
1 bob jones 20
2 charlie joneson 30
3 daisy white 35

In [10]:
def split_name(name):
    first_name, last_name = name.split(' ')
    
    return pd.Series({
        'first_name': first_name,
        'last_name': last_name
    })

df_new = df['name'].apply(split_name)

df_final = pd.concat([df,df_new],axis=1)
df_final


Out[10]:
name age first_name last_name
0 alice smith 25 alice smith
1 bob jones 20 bob jones
2 charlie joneson 30 charlie joneson
3 daisy white 35 daisy white

simple series


In [11]:
s = pd.Series(['foo','bar','baz'])

In [12]:
s.str.capitalize()


Out[12]:
0    Foo
1    Bar
2    Baz
dtype: object