baxx
baxx

Reputation: 4725

Split column in pandas and retain the first split

The following will work:

s = pd.Series(["one_here_there", "two_here_there", "there_there_here"])
s.str.split("_", n=1).transform(lambda x: x[0])

I'm wondering if there's something more "built in", that avoids the use of lambda, as I've heard they are often abused.

Upvotes: 0

Views: 80

Answers (5)

Akhilesh_IN
Akhilesh_IN

Reputation: 1327

add expand=True and take first column

s.str.split("_",expand=True)[0]

ouptut:

Out[15]:
0      one
1      two
2    there
Name: 0, dtype: object

Upvotes: 0

THG
THG

Reputation: 322

You are looking for the itemgetter function.

Use it like so:

import operator

get_first = operator.itemgetter(0)
s = pd.Series(["one_here_there", "two_here_there", "there_there_here"])

s.str.split("_", n=1).transform(get_first)

Upvotes: 1

rhug123
rhug123

Reputation: 8778

Yes, you do not need to use lambda here.

You can use

s.str.split("_", n=1).str[0]

Or you can use

s.str.split("_", n=1).str.get(0)

Upvotes: 2

BENY
BENY

Reputation: 323346

Yes we have str

s.str.split("_", n=1).str[0]

Upvotes: 2

wwnde
wwnde

Reputation: 26676

Please try .str.split().str[index]

s.str.split("_").str[0]

Upvotes: 2

Related Questions