Stack Exchange
Iterating on value counts of a column in dataframe
I have this dataframe:
power = [0,1,2,9,9,9,9,9,9,9,8,3]
df_p = pd.DataFrame(power, columns = ['power'])

    power
0   0
1   1
2   2
3   9
4   9
5   9
6   9
7   9
8   9
9   9
10  8
11  3

Here I took the value count of power column and then again make a df of this where power and respective value counts is showing. 
vc_d = df_p.power.value_counts()
vc_d = pd.DataFrame (vc_d)
vc_d = vc_d.reset_index()
vc_d.columns =['power', 'counts']

    power   counts
0   9       7
1   8       1
2   3       1
3   2       1
4   1       1
5   0       1

This is just a sample, i have around 70000 entries. And i want to iterate on the power values to check it's percentage occurance in DF. For example:
vc_d.loc[ vc_d ['power'] > 8 , 'counts'].sum() / len(power) *100

out[] 58.333333333333336

vc_d.loc[ vc_d ['power'] > 9 , 'counts'].sum() / len(power) *100

out[] 0.0

Above can be seen that almost 58% of my power values are greater than > 8, but as soon as i give > 9 it's 0.0. Desired output is to get the value '8'. This means if i go further than the 8 the power values are not existing or less in numbers this will decrease the percentage. 
Any idea how to do this? Thanks in advance.

vc_d.loc[1,'power']?
is that really this will always be fulfilled ...

It doesn't matter. I need to find the power (which is in this case 8). After iterate i can find that power grater than 9 is 0.0 percent and power greater then 8 are in 58.00. This means that 58.00 percent of my power is  9 ( >8).

@anky_91 thanks for the answer. i want to find the threshold where the percentage of value count changes, let's say about 50%. It means the power greater than 8 are in 58% and if i go further to 9 than it's zero. This means the values of power are greater than 8 but less than 9.

@Arpit can you try df_p.power.value_counts().pct_change().fillna(0).abs().gt(.5).idxmax() ? where .5 is threshold

Considering threshold as 0.5 , you can use pct_change() to see percentage change across a series and apply your logic:
df_p.power.value_counts().pct_change().fillna(0).abs().gt(.5).idxmax()
#8

Where:
df_p.power.value_counts().pct_change().fillna(0).abs()

Gives:
9    0.000000
8    0.857143
3    0.000000
2    0.000000
1    0.000000
0    0.000000

