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?
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With