Esta es la respuesta más válida que te podemos dar, pero obsérvala detenidamente y analiza si se adapta a tu proyecto.
from itertools import chain
dataframe['features'] = dataframe.apply(lambda x: ''.join([*chain.from_iterable((v, f' ') for i, v in enumerate(x))][:-1]), axis=1)
print(dataframe)
Huellas dactilares:
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
Puedes usar df.agg
para unir las columnas del marco de datos pasando el parámetro opcional axis=1
. Utilizar:
df['features'] = df.agg(
lambda s: r' <> '.join(s).format(*range(s.size)), axis=1)
Producción:
# 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
def join_(value):
vals = []
for i, j in enumerate(value):
vals.append(j + " <%d>" % i if i < len(value) - 1 else j)
return " ".join(vals)
# setting axis=1 will pass all columns to the join_ func.
dataframe['featurs'] = dataframe.apply(lambda x: join_(x), axis=1)
print(dataframe)
Producción
col_1 col_2 col_3 featurs
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
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)