Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a new column from each row in pandas

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?

like image 974
superwelling Avatar asked Dec 20 '25 15:12

superwelling


2 Answers

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

like image 66
Kevin Ramnauth Avatar answered Dec 22 '25 06:12

Kevin Ramnauth


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
like image 20
Ryan Avatar answered Dec 22 '25 04:12

Ryan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!