2

I have a dataframe with column like:

col 
a2_3
f4_4
c4_1

I want to add two columns from this column, like so:

col   col1   col2   col3
a2_3    a      2     3
f4_4    f      4     4
c4_1    c      4     1

The following does not work:

df[['col1', 'col2', 'col3']] = df['col'].apply(lambda s: (s[1], *s[1:].split("_")) )

How can I assign series of tuples to new columns?

1 Answer 1

3

Here apply is not necessary, first indexing with str and then use Series.str.split with expand=True:

df[['col1', 'col2']] = df['col'].str[1:].str.split("_", expand=True)
print (df)
    col col1 col2
0  a2_3    2    3
1  f4_4    4    4
2  c4_1    4    1

Your solution is possible with Series constructor, but it is slow:

df[['col1', 'col2']] = df['col'].apply(lambda s: pd.Series(s[1:].split("_")))

Faster is use DataFrame constructor:

df1 = pd.DataFrame(df['col'].apply(lambda s: s[1:].split("_")).tolist(), index=df.index)
df[['col1', 'col2']] = df1

Or list comprehension:

df[['col1', 'col2']] = pd.DataFrame([s[1:].split("_") for s in df['col']], index=df.index)

EDIT: Solution is similar:

L = df['col'].apply(lambda s: (s[0], *s[1:].split("_"))).tolist()
df[['col1', 'col2', 'col3']] = pd.DataFrame(L, index=df.index)

df[['col1', 'col2', 'col3']] = pd.DataFrame([(s[0], *s[1:].split("_")) for s in df['col']], 
                                 index=df.index)
print (df)
    col col1 col2 col3
0  a2_3    a    2    3
1  f4_4    f    4    4
2  c4_1    c    4    1
Sign up to request clarification or add additional context in comments.

2 Comments

wrapping in pd.Series is VERY slow. (100K rows), without pd.Series it is fairly fast. Is there a way to make it faster and still use apply?
Edited the question, I need the first letter as well as a third column

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.