简体   繁体   中英

Create a new column to data frame from existing columns using Pandas

I am not sure how to build a data frame here but I am looking for a way to take the data from multiple columns and combine them into 1 column. Not as a sum but as a joined value.

Ex. MB|Val|34567|W123 -> MB|Val|34567|W123|MB_Val_34567_W123.                                          

What I have tried so far is creating a conditions variable that calls a particular column identical to the value in it

conditions = [(Groupings_df['GroupingCriteria1'] == 'MB')]

then a values variable that would include what I want in the new column

values = ['MB_Val_34567_W123']

and lastly grouping it

Groupings_df['GroupingColumn'] = np.select(conditions,values)

This works for 1 row but it would be inefficient to keep manually changing the number in the values variable (34567) over a df with thousands of rows

IIUC, you want to create a new column as a concatenation of each row:

df = pd.DataFrame({'A': ['MB'], 'B': ['Val'], 'C': [34567], 'D': ['W123']})
df['E'] = df.astype(str).apply(lambda x: '_'.join(x), axis=1)
print(df)

# Output
    A    B      C     D                  E
0  MB  Val  34567  W123  MB_Val_34567_W123

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