Reputation: 105
I have this following df:
d = {'role': ['admin/tech', 'admin', 'programming/asst/design']}
df = pd.DataFrame(data=d)
My goal is:
In other words, extract everything before the first '/' IF row contains '/'
I came up with:
and now I need to add a IF statement for this function
I tried something like:
But it doesn´t work.
Thanks in advance, I really appreciate suggestions!!
Upvotes: 0
Views: 491
Reputation: 23146
You don't need a lambda
function for what you want to achieve.
Try this:
>>> df["role"].str.split("/").str[0]
0 admin
1 admin
2 programming
Name: role, dtype: object
If for whatever reason you still want to use lambda
, do this:
>>> df["role"].apply(lambda x: x.split("/")[0])
0 admin
1 admin
2 programming
Name: role, dtype: object
Upvotes: 2