简体   繁体   中英

Creating new column in a Pandas dataframe by concatenating two other column based on some condition

I have a dataframe as shown below (only showing relevant columns):

col1          col2
NaN           NaN
NaN           abc@123.com | pqr@234.com
ppp@987.com   NaN
zzz@765.com   aaa@123.com | mmm@456.com

I want to create a new column based on concatenating col1 and col2 with " | " as a separator. NaN values should be ignored

Expected output:

col1          col2                          new_col
NaN           NaN                           NaN
NaN           abc@123.com | pqr@234.com     abc@123.com | pqr@234.com
ppp@987.com   NaN                           ppp@987.com
zzz@765.com   aaa@123.com | mmm@456.com     zzz@765.com | aaa@123.com | mmm@456.com

Let us try stack

df['New']=df.stack().groupby(level=0).agg('|'.join)

df
          col1                     col2                                  New
0          NaN                      NaN                                  NaN
1          NaN  abc@123.com|pqr@234.com              abc@123.com|pqr@234.com
2  ppp@987.com                      NaN                          ppp@987.com
3  zzz@765.com  aaa@123.com|mmm@456.com  zzz@765.com|aaa@123.com|mmm@456.com

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