3

I'm trying to create a new column and populate it using values from each row. I have a column 'Journey' and the new column is 'Origin'.

def getOrigin(journey):
    if " to " in journey:
        return journey.split(" to ")[0]
    else:
        return "No origin"

df['Origin'] = getOrigin(df.Journey)

print(df['Origin'])

If df.Journey is "America to England", then I'd expect df['Origin'] to be 'America', but instead every row of Origin is "No origin". How do I do this?

3 Answers 3

1

I believe you need to map it like so:

df['Origin'] = df.Journey.applymap(getOrigin)

this should apply your function to every item in the Journey column

Sign up to request clarification or add additional context in comments.

1 Comment

You can 100% vectorize with a non-loopy solution... although this is an alternative, I'd still recommend 5 other more pertinent ones before getting to this.
1

This solution is less efficient with a lot more code, but as a beginner, easier to understand maybe... Consistent with the way you tried to solve the problem...!

df = pd.DataFrame(data = {'Journey' : ['england to america', 'peru', 'france to china']})

origin = []
def getOrigin(Journey):
    for i in range(len(Journey)):
        if " to " in Journey[i]:
            origin.append(Journey[i].split(" to ")[0])
        else:
            origin.append("No origin")
return origin



df['Origin'] = getOrigin(df['Journey'])

print (df['Origin'])

0      england
1    No origin
2       france
Name: Origin, dtype: object

Comments

0

str.extract + fillna

df['Origin'] = df['Journey'].str.extract('^(.*?)(?=\s*to)').fillna('No origin')

str.split + fillna

df['Origin'] = df['Journey'].str.split(' to').str[0].fillna('No origin')

List comprehension

df['Origin'] = [
    x.split(' to ')[0] if 'to' in x else 'No origin' for x in df['Journey']
]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.