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

后端 未结 11 1649
梦谈多话
梦谈多话 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条回答
  •  -上瘾入骨i
    2020-12-02 15:03

    If you have even more columns you want to combine, using the Series method str.cat might be handy:

    df["combined"] = df["foo"].str.cat(df[["bar", "new"]].astype(str), sep="_")
    

    Basically, you select the first column (if it is not already of type str, you need to append .astype(str)), to which you append the other columns (separated by an optional separator character).

提交回复
热议问题