Reputation: 3713
Given the following data frame:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'A' : ['a', 'b','c', 'd'],
'B' : ['Y>`abcd', 'abcd','efgh', 'Y>`efgh']
})
df
A B
0 a Y>`abcd
1 b abcd
2 c efgh
3 d Y>`efgh
I'd like to split column A on '>`' into 2 columns (C and D) so my data
frame looks like this:
A C D
0 a Y abcd
1 b abcd
2 c efgh
3 d Y efgh
Thanks in advance!
Upvotes: 1
Views: 760
Reputation: 29
The simplest and most memory efficient way of doing it is:
df[['C', 'D']] = df.B.str.split('>`', expand=True)
Upvotes: 0
Reputation: 862481
You can use str.extract
with fillna
, last drop column B
by drop
:
df[['C','D']] = df['B'].str.extract('(.*)>`(.*)', expand=True)
df['D'] = df['D'].fillna(df['B'])
df['C'] = df['C'].fillna('')
df = df.drop('B', axis=1)
print df
A C D
0 a Y abcd
1 b abcd
2 c efgh
3 d Y efgh
Next solution use str.split
with mask
and numpy.where
:
df[['C','D']] = df['B'].str.split('>`', expand=True)
mask = pd.notnull(df['D'])
df['D'] = df['D'].fillna(df['C'])
df['C'] = np.where(mask, df['C'], '')
df = df.drop('B', axis=1)
Timings:
In large DataFrame
is extract
solution 100
times faster, in small 1.5
times:
len(df)=4
:
In [438]: %timeit a(df)
100 loops, best of 3: 2.96 ms per loop
In [439]: %timeit b(df1)
1000 loops, best of 3: 1.86 ms per loop
In [440]: %timeit c(df2)
The slowest run took 4.44 times longer than the fastest. This could mean that an intermediate result is being cached
1000 loops, best of 3: 1.89 ms per loop
In [441]: %timeit d(df3)
The slowest run took 4.62 times longer than the fastest. This could mean that an intermediate result is being cached
1000 loops, best of 3: 1.82 ms per loop
len(df)=4k
:
In [443]: %timeit a(df)
1 loops, best of 3: 799 ms per loop
In [444]: %timeit b(df1)
The slowest run took 4.19 times longer than the fastest. This could mean that an intermediate result is being cached
100 loops, best of 3: 7.37 ms per loop
In [445]: %timeit c(df2)
1 loops, best of 3: 552 ms per loop
In [446]: %timeit d(df3)
100 loops, best of 3: 9.55 ms per loop
Code:
import pandas as pd
df = pd.DataFrame({
'A' : ['a', 'b','c', 'd'],
'B' : ['Y>`abcd', 'abcd','efgh', 'Y>`efgh']
})
#for test 4k
df = pd.concat([df]*1000).reset_index(drop=True)
df1,df2,df3 = df.copy(),df.copy(),df.copy()
def b(df):
df[['C','D']] = df['B'].str.extract('(.*)>`(.*)', expand=True)
df['D'] = df['D'].fillna(df['B'])
df['C'] = df['C'].fillna('')
df = df.drop('B', axis=1)
return df
def a(df):
df = pd.concat([df, df.B.str.split('>').apply(
lambda l: pd.Series({'C': l[0], 'D': l[1][1: ]}) if len(l) == 2 else \
pd.Series({'C': '', 'D': l[0]}))], axis=1)
del df['B']
return df
def c(df):
df[['C','D']] = df['B'].str.split('>`').apply(lambda x: pd.Series(['']*(2-len(x)) + x))
df = df.drop('B', axis=1)
return df
def d(df):
df[['C','D']] = df['B'].str.split('>`', expand=True)
mask = pd.notnull(df['D'])
df['D'] = df['D'].fillna(df['C'])
df['C'] = np.where(mask, df['C'], '')
df = df.drop('B', axis=1)
return df
Upvotes: 3
Reputation: 31161
I would use a one liner:
df['B'].str.split('>`').apply(lambda x: pd.Series(['']*(2-len(x)) + x))
# 0 1
#0 Y abcd
#1 abcd
#2 efgh
#3 Y efgh
Upvotes: 1
Reputation: 76297
Performing a str.split
followed by an apply
returning a pd.Series
will create the new columns:
>>> df.B.str.split('>').apply(
lambda l: pd.Series({'C': l[0], 'D': l[1][1: ]}) if len(l) == 2 else \
pd.Series({'C': '', 'D': l[0]}))
C D
0 Y abcd
1 abcd
2 efgh
3 Y efgh
So you can concat
this to the DataFrame, and del
the original column:
df = pd.concat([df, df.B.str.split('>').apply(
lambda l: pd.Series({'C': l[0], 'D': l[1][1: ]}) if len(l) == 2 else \
pd.Series({'C': '', 'D': l[0]}))],
axis=1)
del df['B']
>>> df
A C D
0 a Y abcd
1 b abcd
2 c efgh
3 d Y efgh
Upvotes: 2