Reputation: 4725
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
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
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
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