PaulCoder
PaulCoder

Reputation: 27

Count the number of clients that have bought in different stores in my dataframe

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?.

Upvotes: 0

Views: 240

Answers (2)

user17242583
user17242583

Reputation:

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

Upvotes: 1

not_speshal
not_speshal

Reputation: 23146

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

Upvotes: 0

Related Questions