0

import pandas as pd

df[["First Name", "Last Name"]] = df["Full Name"].str.split(' ', 1, expand=True)

enter image description here

as you see in this image that it add the split columns at the end of the dataset and i want them to be at same position. Where the (Full Name) located

can anyone solve this problem in easy way

The problem was that it will be added at end of the dataset and I don't want this.

Biny Kan
  • 1
  • 5

1 Answers1

0

Here is one way that you could add the two split columns into your dataframe in the position that you want:

# Import pandas 
import pandas as pd

# Load the dataset
df = pd.read_excel(r"path/to/sample_data.xlsx")

# Make a copy of the dataframe
df2 = df.copy()

# Drop the column you want to split from the original dataframe
df=df.drop(columns = ["Full Name"])

# Split the Full Name column into first name and last name in df2
df2[["First Name", "Last Name"]] = df2["Full Name"].str.split(' ', 1, expand=True)
   
# Insert the new columns at positions 1 and 2
df.insert(1, "First Name", df2["Fist Name"])
df.insert(2, "Last Name", df2["Last Name"])
sloppypasta
  • 1,068
  • 9
  • 15
Biny Kan
  • 1
  • 5