I have below dataframe and I am trying to split 'name' column into first_name and last_name on the basis of space however for some names there is no delimiter and in such cases I want to take the value in last name and have blank in first name.
One possible way is to iterate over all the rows and use if-else condition over each row however as mentioned in this post.
"Iteration in Pandas is an anti-pattern and is something you should only do when you have exhausted every other option." so I am looking for a way to achieve this in Pandas.
names_df = pd.read_csv(io.BytesIO(obj['Body'].read()))
print(names_df)
names_df[['first_name', 'last_name']] = names_df['name'].str.split(' ', expand=True)
print(names_df)
ValueError: Columns must be same length as key
order_id name product_id product_price
0 Thanos Ipad 800
1 Hulk AC 400
2 C America Ipad 760
3 Black Panther IPhone 1100
Expected Dataframe:
first_name last_name
Thanos
Hulk
C America
Black Panther