Index Bra Obr Zal Uto Str Nah Tec Ryc Hla BestP_A BestP_Amin BestP_minA
0 461 38 44 46 49 137 324 322 162 Bra Obr 137.0
1 442 32 30 35 39 180 322 325 180 Bra Obr 180.0
2 152 28 23 23 30 175 335 355 206 Bra Obr 175.0
3 38 33 68 33 49 119 223 46 46 Zal Obr 46.0
4 36 33 203 36 46 217 253 166 170 Zal Obr 166.0
5 35 84 38 41 54 49 175 57 141 Obr Nah 49.0
6 34 45 71 45 59 72 207 57 60 Zal Obr 57.0
熊猫的价值
我需要返回列名为 "BestP_Amin",Python返回的列名不好。例如索引值为0。
BestP_A = Bra, Bra = 461, Nah = 137, Tec = 324, Ryc = 322, Hla = 162
code:
if data.at[i,'BestP_A'] == 'Bra':
data['BestP_Amin'] = data[['Bra','Nah','Tec','Ryc','Hla']].idxmin(axis=1)
返回值Obr
,问题出在哪里?
区块代码。
# nalezení nejleší pozice, přepočet podle výše atributů
for i in range(0,len(data.index)):
# nalezení pozice
data['BestP_A'] = data[['Bra','Obr','Zal','Uto']].idxmax(axis=1)
#nalezení nejmenšího atributu
# VRACI CHYBNE HODNOTY
if data.at[i,'BestP_A'] == 'Bra':
data['BestP_Amin'] = data[['Bra','Nah','Tec','Ryc','Hla']].idxmin(axis=1)
elif data.at[i,'BestP_A'] == 'Obr':
data['BestP_Amin'] = data[['Obr','Nah','Tec','Ryc','Hla']].idxmin(axis=1)
elif data.at[i,'BestP_A'] == 'Zal':
data['BestP_Amin'] = data[['Zal','Nah','Tec','Ryc','Hla']].idxmin(axis=1)
elif data.at[i,'BestP_A'] == 'Uto':
data['BestP_Amin'] = data[['Uto','Nah','Tec','Ryc','Hla']].idxmin(axis=1)
data.at[i,'BestP_minA'] = min(data.at[i,'Nah'],data.at[i,'Tec'],data.at[i,'Ryc'],data.at[i,'Hla'])
data.at[i,'BestA_U'] = int(round(data.at[i,'BestA']*(data.at[i,'Ene']/100)*(1+(.25*data.at[i,'Sou']/100)+(.2*data.at[i,'Zku']/100)),0))
Block code in python:
3 个回答
0 人赞同
我不知道你为什么要使用for
的循环,但是如果你的目标是获得那些表示数值最小的列之间的名称,单单这一行就足够了。
df['BestP_Amin'] = df[['Bra','Nah','Tec','Ryc','Hla']].idxmin(axis=1)
不需要用那些for
的语句把它放在一个if
的循环里。idxmin
已经对每一行进行了矢量搜索,产生的列作为结果。
在这之后,该列看起来像。
0 Nah
1 Nah
2 Bra
3 Bra
4 Bra
5 Bra
6 Bra
0 人赞同
嗯,循环 "For "必须用在 "If "上。需要测试 "BestP_A "中的值并改变填充 "BestP_Amin "的选择。
0 人赞同
Solved, use tuple
for i in range(0,len(data.index)):
if data.at[i,'BestP_A'] == 'Bra':
polemin = [data.loc[i,'Bra'],data.loc[i,'Nah'],data.loc[i,'Tec'],data.loc[i,'Ryc'],data.loc[i,'Hla']]