1

I have a pandas dataframe which looks like this,

ID t_payment t_process t_service t_others
1   0          1          1        0
2   1          0          0        0
3   0          1          1        1
4   1          1          1        1
5   1          1          0        0

I am trying to convert this to single variable and the output should look like this,

ID category
1  process,service
2  payment
3  process,service,others
4  payment,process,service,others
5  payment,process

How can i do this? I can use if else logic, but i have more than 8 columns, covering all the possible combination looks tedious, is there any other approach?

Thanks in advance.

EDIT : Whoever closed this as a duplicate, it is not the same as the other question. Here I have multiclasses, if you look at first record for example, both t_process & t_service is true. However in the question you pointed, only one class is true for a row. Also here there is an ID column.

ds_user
  • 2,139
  • 4
  • 36
  • 71
  • 1
    Use `pd.get_dummies(category)` – Sumanth Feb 19 '19 at 08:05
  • I think you misunderstood the question, if you look at it, i want to do the reverse. Create category column from the classes. (encoded one). – ds_user Feb 19 '19 at 08:39
  • Try this `i, j = np.where(df)` and then `df = pd.Series(dict(zip(zip(i, j), df.columns[j]))).reset_index(-1, drop=True)` and then groupby index values to get a series `df.groupby(df.index).unique()` – Sumanth Feb 20 '19 at 06:21

0 Answers0