Find the proportion of each value in the column using pandas in python
滿天的星座
滿天的星座 2017-05-18 10:55:04
0
2
1858

For example, I have a dataframe called df

   Prices  Amount Action  C
0       3      57   Sell  1
1      89      42   Sell  1
2      45      70    Buy -1
3       6      43   Sell  1
4      60      47   Sell  1
5      19      16    Buy -1
6      56      89   Sell  1
7       3      28    Buy -1
8      56      69   Sell  1
9      90      49    Buy -1

The method currently used is

df['Prices'].apply(lambda x :x/df['Prices'].sum())

It feels very slow. Is there any faster way?

滿天的星座
滿天的星座

reply all(2)
刘奇

willsum()写在lambda表达式外面,否则每次计算一个x占该列的比例时,又需要重新计算sum(), wasting a lot of time.

某草草
df['Prices'] = df['Prices'] / df['Prices'].sum()
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template