0

I have this following df:

d = {'role': ['admin/tech', 'admin', 'programming/asst/design']}
df = pd.DataFrame(data=d)

enter image description here

My goal is:

enter image description here

In other words, extract everything before the first '/' IF row contains '/'

I came up with:

enter image description here

and now I need to add a IF statement for this function

I tried something like:

enter image description here

But it doesn´t work.

Thanks in advance, I really appreciate suggestions!!

3
  • 2
    Please don't post code as screenshots. Commented Jul 13, 2021 at 16:04
  • 1
    Python's conditional expressions need an else statement, hence the error. Otherwise what should happen if x.lower is not True? docs.python.org/2.5/whatsnew/pep-308.html Commented Jul 13, 2021 at 16:05
  • 1
    I strongly believe it's some IDE error. the code on terminal is different from code on text editor. delete and rewrite the line might work. Commented Jul 13, 2021 at 16:06

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.