Plotting pandas groupby results

user201411

I have a very simple data set:

Customer    Amount
A           1.25
B           2
C           1
A           5
D           2
B           10

I would like to get the following result:

Customer Amount  Number_of_transactions
A        6.25    2
B        12      2
C        1       1
D        1       2

The way I solved is to add another column where all values are 1 and then use df.groupby('Customer').

  1. Is there a more efficient way to do that?
  2. I would need to plot the distribution of number_of_transactions and distribution of amounts. Whenever I try to do that, I get key error (I assume because of groupby). Can someone point to the right direction?
elyase

Try this:

>>> df['Number_of_transactions'] = 1
>>> df1 = df.pivot_table(index='Customer', 
                         values=['Amount', 'Number_of_transactions'],
                         aggfunc=np.sum)\
            .reset_index()                  # reset_index is optional
>>> df1

Out[21]:
    Customer    Amount  Number_of_transactions
0   A           6.25    2
1   B           12.00   2
2   C           1.00    1
3   D           2.00    1

For the plots just do:

>>> df1.hist(bin=50)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Plotting with GroupBy in Pandas/Python

From Dev

Pandas Multicolumn Groupby Plotting

From Dev

Python pandas plotting and groupby

From Dev

groupby multiple values, and plotting results

From Dev

Pandas - count groupBy results

From Dev

Plotting groupby with Pandas vs. Matplotlib

From Dev

Plotting multiple time series after a groupby in pandas

From Dev

Plotting Pandas groupby groups using subplots and loop

From Dev

Plotting Pandas OLS linear regression results

From Dev

How to filter results of a groupby in pandas

From Dev

Python pandas sort groupby results

From Dev

Error in filtering groupby results in pandas

From Dev

Pandas groupby year object plotting it year over year

From Dev

Pandas groupby year object plotting it year over year

From Dev

How to plot aggregate results after groupby in Pandas?

From Dev

Filter pandas data frame with results of groupby

From Dev

Display Pandas groupby results for defined groups only

From Dev

How to plot aggregate results after groupby in Pandas?

From Dev

missing key column in results of pandas groupby filter

From Dev

Pandas how to calculate results based groupby

From Dev

Getting Top N results from pandas groupby

From Dev

plotting sympy results in python

From Dev

Weird Plotting Results

From Dev

Plotting the results of a For Loop in R

From Dev

output file with names from groupby results pandas python

From Dev

append pandas.DataFrame.GroupBy results into another dataframe

From Dev

Pandas modify column with groupby results while ignoring some values

From Dev

How do I turn pandas DataFrame groupby results into a DataFrame?

From Dev

pandas value_counts with bins applied to a groupby produces incorrect results

Related Related

HotTag

Archive