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').
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.
Comments