Hello, everyone It's been a while since I asked you a question. In the next picture, the expression in the "for" statement Can we turn it back to the For Moon one more time instead of like that?
What can I do if I want to go from one for sentence to another and store it in new variables?
ff = []
for i in range(len(score)):
a = score["AMT_IND"][i]*score.iloc[i,5]
b = score["AMT_IND"][i]*score.iloc[i,6]
c = score["AMT_IND"][i]*score.iloc[i,7]
d = score["AMT_IND"][i]*score.iloc[i,8]
e = score["AMT_IND"][i]*score.iloc[i,9]
f = score["AMT_IND"][i]*score.iloc[i,10]
g = score["AMT_IND"][i]*score.iloc[i,11]
h = score["AMT_IND"][i]*score.iloc[i,12]
n = score["OPER_DT"][i]
m = score["ADMD"][i]
k = score["Day"][i]
row = pd.DataFrame([n,m,a,b,c,d,e,f,g,h,k]).T
row.columns = [ "OPER_DT", "ADMD", "LCLS_10_P", "LCLS_20_P", "LCLS_30_P",
"LCLS_40_P", "LCLS_50_P", "LCLS_60_P",
"LCLS_70_P", "LCLS_80_P", "DAY", ]
ff.append(row)
If you calculate each row of the pandas data frame by turning it into a for statement like this, the performance will not be good.
Looking at the intention of the code, the key is to multiply the "AMT_IND"
column of the score
data frame by the 5th to 12th columns to create columns such as "LCLS_10_P",...
.
You don't need a for door at all.
For example, if the fifth column is named "AAA"
, the entire "LCLS_10_P"
column is created in one line as follows:
score["LCLS_10_P"] = score["AMT_IND"] * score["AAA"]
You can create all the "LCLS_XX_P"
columns and then write down the columns you need. If you use it as a column name, the order doesn't matter.
© 2024 OneMinuteCode. All rights reserved.