Martynelius
Martynelius

Reputation: 45

Split two columns in a pandas dataframe into two and name them

I have this pandas dataframe

         x           y       Values
0       A B         C D       4.7
1       A B         C D       10.9
2       A B         C D       1.8
3       A B         C D       6.5
4       A B         C D       3.4

I would like to split the x and y columns and get an output with these given names on the columns.

     x    f    y    g   Values
0    A    B    C    D    4.7
1    A    B    C    D    10.9
2    A    B    C    D    1.8
3    A    B    C    D    6.5
4    A    B    C    D    3.4

Is there a straight forward way to do this in python?

Upvotes: 3

Views: 62

Answers (1)

df[['x', 'f']] = df.x.str.split(" ", expand=True)
df[['y', 'g']] = df.y.str.split(" ", expand=True)
df[['x','f','y','g', 'Values']]

You can make it scalable as well, defining a dict in which the keys are the columns, and the values a list with the desired new column names:

# Define the target columns to split, and their new column names
cols={
    'x': ['x','f'],
    'y': ['y','g']
}
# Apply the function to each target-column
for k in cols:
    df[cols[k]] = df[k].str.split(" ", expand=True)

# Reorder the dataframe as you wish
new_columns = sum(cols.values(),[])
old_columns = set(df.columns) - set(new_columns)
df[new_columns + list(old_columns)]

Upvotes: 4

Related Questions