我很难找到一种有效的方法来计算列值大于先前列的次数。仅BC&D列应获得分数。我需要计算给定列中的值大于其左边任何列的次数。即(“ C得分”会比较A和B列中的值)
我尝试了几种列表理解版本,但是似乎没有什么是正确的答案或可用的格式。
import numpy as np
import pandas as pd
np.random.seed(10)
df = pd.DataFrame(np.random.randint(0,100,size=(10, 4)), columns=list('ABCD'))
df['B Score'] = ??
df['C Score'] = ??
df['D Score'] = ??
df_result = df
BScores = [1,1,0,0,0,1,0,0,0,0]
CScores = [2,0,1,2,2,2,1,0,1,0]
DScores = [2,0,1,3,3,3,0,0,1,0]
df_result['B Score'] = BScores
df_result['C Score'] = CScores
df_result['D Score'] = DScores
感谢阅读!
答案 0 :(得分:1)
这就是我要做的:
cols = df.columns.values
for i in range(1,len(cols)):
col = cols[i]
df[f'{col}_score'] = df.iloc[:,:i].lt(df.iloc[:,i], axis='index').sum(1)
数据:
np.random.seed(1)
df = pd.DataFrame(np.random.randint(0,100,size=(10, 4)), columns=list('ABCD'))
输出:
A B C D B_score C_score D_score
0 37 12 72 9 0 2 0
1 75 5 79 64 0 2 1
2 16 1 76 71 0 2 2
3 6 25 50 20 1 2 1
4 18 84 11 28 1 0 2
5 29 14 50 68 0 2 3
6 87 87 94 96 0 2 3
7 86 13 9 7 0 0 0
8 63 61 22 57 0 0 1
9 1 0 60 81 0 2 3