0

My data frame looks like -

name        clicked
    a            1
    b            0
    c            0
    c            0
    d            0
    a            1
    a            0
    b            1
    b            0

my final data frame looks like -

name      no_clicked_1         no_clicked_0
a             2                     0
b             1                     2
c             1                     1
d             0                     1

We can do groupby on name for each 1 and 0. Then merge those data frame. Is there any way we can do this thing in pandas.

Nikita Agarwal
  • 343
  • 1
  • 3
  • 13
  • `final=pd.crosstab(df['name'],df['clicked'])` and `final.add_prefix(f"no_{m.columns.name}_")` – anky Dec 23 '19 at 07:30
  • @anky_91 - thanks for your quick reply..but getting this error - AttributeError: 'DataFrame' object has no attribute 'crosstab' – Nikita Agarwal Dec 23 '19 at 07:36
  • No problem , dataframe has no such attributes, use `pd.crosstab()` as shown above, or follow solution linked in the dupe, both solves your problem – anky Dec 23 '19 at 07:36
  • @anky_91 - i have done ..final=pd.crosstab(df_train['user_offer_merchant'],df_train['rating']) final.add_prefix(f"no_{m.columns.name}_")...getting error - NameError: name 'm' is not defined – Nikita Agarwal Dec 23 '19 at 07:44
  • My bad replace `m` with `final` , also execute them as separate lines – anky Dec 23 '19 at 07:45
  • @anky_91 - this line is not reflecting - final.add_prefix(f"no_{final.columns.name}_") and also i think reset_index() is needed otherwise column name is not properly showing – Nikita Agarwal Dec 23 '19 at 07:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204710/discussion-between-anky-91-and-nikita-agarwal). – anky Dec 23 '19 at 07:51

0 Answers0