Reputation: 137
I have a df:
FROM TO
0 LA:Los_Angeles NY:New_York
1 NY:New_York LA:Los_Angeles
I want to combine parts of the text into a new column like so:
FROM TO MERGED
0 LA:Los_Angeles NY:New_York LA->NY
1 NY:New_York LA:Los_Angeles NY->LA
So far my code is:
df = pd.DataFrame({'FROM':['LA:Los_Angeles', 'NY:New_York'], 'TO':['NY:New_York', 'LA:Los_Angeles']})
print(df)
df['MERGED'] = str(df['FROM']).split(':')[0] + '->' + str(df['TO']).split(':')[0]
print(df)
Which gives back:
FROM TO MERGED
0 LA:Los_Angeles NY:New_York 0 LA->0 NY
1 NY:New_York LA:Los_Angeles 0 LA->0 NY
It seems to only be grabbing the first row and performing the operation. It also seems to be including the index in the string somehow, the '0' leading the city. Not too sure where I am going wrong.
Upvotes: 1
Views: 1465
Reputation: 367
Try this
df['MERGED'] = df['FROM'].str.split(':').str.get(0) + '->' +df['TO'].str.split(':').str.get(0)
FROM TO MERGED
0 LA:Los_Angeles NY:New_York LA->NY
1 NY:New_York LA:Los_Angeles NY->LA
Upvotes: 1