简体   繁体   中英

pandas apply and assign to multiple columns

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM