Reputation: 147
I can't get result from column ['a percent'], only column ['b percent'] get filled. can someone help ? or any reference link will be helpfull ? many thanks
test = pd.DataFrame({'a':[20,2,7,1,7,7,34],'b':[100,99,102,103,56,70,200]})
test['a percent'] = ''
test['b percent'] = ''
x = ['a','b']
for row in test.index:
for x in x:
test.loc[row,f'{x} percent'] = stats.percentileofscore(test[x],test.loc[row,x])/100
output:
a b a percent b percent
0 20 100 0.857143 0.571429
1 2 99 0.428571
2 7 102 0.714286
3 1 103 0.857143
4 7 56 0.142857
5 7 70 0.285714
6 34 200 1.0
Upvotes: 1
Views: 90
Reputation: 261590
You overwrite your x
in for x in x
, use a different name:
cols = ['a','b']
for row in test.index:
for x in cols:
test.loc[row,f'{x} percent'] = stats.percentileofscore(test[x],test.loc[row,x])/100
That said a better, vectorial method would be:
cols = ['a', 'b']
test.update(test[cols].apply(lambda c: stats.percentileofscore(c, c))
.div(100).add_suffix(' percent'))
Output:
a b a percent b percent
0 20 100 0.857143 0.571429
1 2 99 0.285714 0.428571
2 7 102 0.571429 0.714286
3 1 103 0.142857 0.857143
4 7 56 0.571429 0.142857
5 7 70 0.571429 0.285714
6 34 200 1.0 1.0
Upvotes: 2