2021-12-10

Tech Programing

程式人小天地

python: concatenate unknown number of columns in pandas DataFrame

1 min read


I need to concatenate some columns in a pandas DataFrame with “_” as separator and store the result in a new column in the same DataFrame. The problem is that I don’t know in advance which and how many columns to concatenate. The labels of the columns to be concatenated are determined at run time of the program and stored in a list.

Example:

import pandas as pd

df=pd.DataFrame(data={'col.a':['a','b','c'],'col.b':['d','e','f'], 'col.c':['g','h','i']})

  col.a col.b col.c
0     a     d     g
1     b     e     h
2     c     f     i

cols_to_concat = ['col.a','col.c']

Desired result:

  col.a col.b col.c cols.concat
0     a     d     g         a_g
1     b     e     h         b_h
2     c     f     i         c_i

I need a method for generating df[‘cols.concat’] that works for a df with any number of columns and where cols_to_concat is an arbitrary subset of df.columns.



Source link

資料來源:Stackoverflow

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *