mercredi 15 juillet 2020

Is there a python way to merge multiple cells with condition

I needed to search multiple cells for a specific value and when it is found it should be returned in new column.

I got an answer here; Python: find string in multiple columns and return it in new column yet this line below return the first value found

df['b'] = (df[cols].where(df[cols].stack().str.contains('b')
         .unstack(fill_value=False)).ffill(1).iloc[:,-1])

where cols

df = df[['col1', 'col2', 'col3', 'col4']]

I tried the other answers and they all gave me error ValueError: cannot reindex from a duplicate axis

Do any one have an idea how can I get all the matching values in one cell.

Aucun commentaire:

Enregistrer un commentaire