client | product | store |
---|---|---|
001 | toy1 | 10 |
001 | toy2 | 20 |
003 | toy3 | 10 |
004 | toy4 | 40 |
001 | toy4 | 30 |
004 | toy4 | 50 |
What I need to do it´s to count the number of clients that have bought in one store, two stores, three stores and more, something like this.
one store | two stores | three stores |
---|---|---|
1 | 1 | 1 |
The purpose of this I´ts to count how many clients buy in different stores, It´s there anyway to this with python?.
CodePudding user response:
Try this:
new_df = df.groupby('client')['store'].count().value_counts().to_frame().sort_index().T.add_suffix(' store').reset_index(drop=True)
Output:
>>> new_df
1 store 2 store 3 store
0 1 1 1
CodePudding user response:
Try:
output = (df.groupby("client")["store"]
.nunique()
.reset_index()
.groupby("store")
.count()
.T
.add_suffix(" store")
.rename_axis(None,axis=1))
>>> output
1 store 2 store 3 store
client 1 1 1