How to concat multiple Pandas DataFrame columns with different token separator?
from itertools import chain
dataframe['features'] = dataframe.apply(lambda x: ''.join([*chain.from_iterable((v, f' <{i}> ') for i, v in enumerate(x))][:-1]), axis=1)
print(dataframe)
Prints:
col_1 col_2 col_3 features
0 aaa name_aaa job_aaa aaa <0> name_aaa <1> job_aaa
1 bbb name_bbb job_bbb bbb <0> name_bbb <1> job_bbb
2 ccc name_ccc job_ccc ccc <0> name_ccc <1> job_ccc
3 ddd name_ddd job_ddd ddd <0> name_ddd <1> job_ddd
You can use df.agg
to join the columns of the dataframe by passing the optional parameter axis=1
. Use:
df['features'] = df.agg(
lambda s: r' <{}> '.join(s).format(*range(s.size)), axis=1)
Output:
# print(df)
col_1 col_2 col_3 features
0 aaa name_aaa job_aaa aaa <0> name_aaa <1> job_aaa
1 bbb name_bbb job_bbb bbb <0> name_bbb <1> job_bbb
2 ccc name_ccc job_ccc ccc <0> name_ccc <1> job_ccc
3 ddd name_ddd job_ddd ddd <0> name_ddd <1> job_ddd