简体   繁体   中英

Generate extra column in a dataframe by concating all the columns whose headers matches with strings in a list using python

I have a dataframe named :- df_explode inputdataframe

I have a list of strings " myList_groupby ".

myList_groupby =  ["domain","tag_name","tag_hierarchy","html_attributes","extension","xyz"]

I want to generate a new column named "combined" in df_explode ,whose elements will be a string concatenation for all the columns whose header matches with strings in myList_groupby list.(is in myList_groupby )

Output dataframe :-

outputdataframe

我认为,需要通过子集过滤列,并呼吁join每行:

df['new'] = df[myList_groupby].apply('_'.join, axis=1)

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