How to concatenate multiple column values into a single column in Panda dataframe

后端 未结 11 1721
梦谈多话
梦谈多话 2020-12-02 14:25

This question is same to this posted earlier. I want to concatenate three columns instead of concatenating two columns:

Here is the combining two columns:

         


        
11条回答
  •  鱼传尺愫
    2020-12-02 15:11

    Another solution using DataFrame.apply(), with slightly less typing and more scalable when you want to join more columns:

    cols = ['foo', 'bar', 'new']
    df['combined'] = df[cols].apply(lambda row: '_'.join(row.values.astype(str)), axis=1)
    

提交回复
热议问题