gpt4 book ai didi

python - 使用字符串通过 "for"语句压缩重复代码 - Python

转载 作者:行者123 更新时间:2023-12-01 03:44:31 25 4
gpt4 key购买 nike

我对 Python 中的“for”语句非常陌生,我无法得到一些我认为应该简单工作的东西。我的代码是:

import pandas as pd

df1 = pd.DataFrame({'Column1' : pd.Series([1,2,3,4,5,6])})
df2 = pd.DataFrame({'Column1' : pd.Series([1,2,3,4,5,6])})
df3 = pd.DataFrame({'Column1' : pd.Series([1,2,3,4,5,6])})

DF1 = pd.DataFrame({'Column1' : pd.Series([1,2,3,4,5,6])})
DF2 = pd.DataFrame({'Column1' : pd.Series([1,2,3,4,5,6])})
DF3 = pd.DataFrame({'Column1' : pd.Series([1,2,3,4,5,6])})

然后:

A1 = len(df1.loc[df1['Column1'] <= DF1['Column1'].iloc[2]])  
Z1 = len(df1.loc[df1['Column1'] >= DF1['Column1'].iloc[3]])

A2 = len(df2.loc[df2['Column1'] <= DF2['Column1'].iloc[2]])
Z2 = len(df2.loc[df2['Column1'] >= DF2['Column1'].iloc[3]])

A3 = len(df3.loc[df3['Column1'] <= DF3['Column1'].iloc[2]])
Z3 = len(df3.loc[df3['Column1'] >= DF3['Column1'].iloc[3]])

正如您所看到的,这是很多重复代码,只是标识号不同。所以我第一次尝试“for”语句是:

Numbers = [1,2,3]

for i in Numbers:
"A" + str(i) = len("df" + str(i).loc["df" + str(i)['Column1'] <= "DF" + str(i)['Column1'].iloc[2]])
"Z" + str(i) = len("df" + str(i).loc["df" + str(i)['Column1'] >= "DF" + str(i)['Column1'].iloc[3]])

这产生了语法错误:“无法分配给运算符”。所以我尝试了:

Numbers = [1,2,3]

for i in Numbers:
A = "A" + str(i)
Z = "Z" + str(i)
A = len("df" + str(i).loc["df" + str(i)['Column1'] <= "DF" + str(i)['Column1'].iloc[2]])
Z = len("df" + str(i).loc["df" + str(i)['Column1'] >= "DF" + str(i)['Column1'].iloc[3]])

这产生了 AttributeError: 'str' 对象没有属性 'loc'。我尝试了其他一些事情,例如:

Numbers = [1,2,3]

for i in Numbers:
A = "A" + str(i)
Z = "Z" + str(i)
df = "df" + str(i)
DF = "DF" + str(i)
A = len(df.loc[df['Column1'] <= DF['Column1'].iloc[2]])
Z = len(df.loc[df['Column1'] <= DF['Column1'].iloc[3]])

但这只会给我带来同样的错误。最终我想要的是这样的:

Numbers = [1,2,3]

for i in Numbers:
Ai = len(dfi.loc[dfi['Column1'] <= DFi['Column1'].iloc[2]])
Zi = len(dfi.loc[dfi['Column1'] <= DFi['Column1'].iloc[3]])

如果我输入以下内容,输出将是等效的:

A1 = len(df1.loc[df1['Column1'] <= DF1['Column1'].iloc[2]])  
Z1 = len(df1.loc[df1['Column1'] >= DF1['Column1'].iloc[3]])

A2 = len(df2.loc[df1['Column1'] <= DF2['Column1'].iloc[2]])
Z2 = len(df2.loc[df1['Column1'] >= DF2['Column1'].iloc[3]])

A3 = len(df3.loc[df3['Column1'] <= DF3['Column1'].iloc[2]])
Z3 = len(df3.loc[df3['Column1'] >= DF3['Column1'].iloc[3]])

最佳答案

在 for 循环中生成变量是“受限”的(您可以这样做,但最好避免。请参阅其他帖子: post_1post_2 )。

而是使用此代码来实现您的目标,而无需生成所需数量的变量(实际上仅生成 for 循环中的值):

# Lists of your dataframes
Hanimals = [H26, H45, H46, H47, H51, H58, H64, H65]
Ianimals = [I26, I45, I46, I47, I51, I58, I64, I65]

# Generate your series using for loops iterating through your lists above
BPM = pd.DataFrame({'BPM_Base':pd.Series([i_a for i_a in [len(i_h.loc[i_h['EKG-evt'] <=\
i_i[0].iloc[0]]) / 10 for i_h, i_i in zip(Hanimals, Ianimals)]]),
'BPM_Test':pd.Series([i_z for i_z in [len(i_h.loc[i_h['EKG-evt'] >=\
i_i[0].iloc[-1]]) / 30 for i_h, i_i in zip(Hanimals, Ianimals)]])})

更新

更有效的方法(仅迭代“动物”列表一次):

# Lists of your dataframes
Hanimals = [H26, H45, H46, H47, H51, H58, H64, H65]
Ianimals = [I26, I45, I46, I47, I51, I58, I64, I65]

# You don't need using pd.Series(),
# just create a list of tuples: [(A26, Z26), (A45, Z45)...] and iterate over it
BPM = pd.DataFrame({'BPM_Base':i[0], 'BPM_Test':i[1]} for i in \
[(len(i_h.loc[i_h['EKG-evt'] <= i_i[0].iloc[0]]) / 10,
len(i_h.loc[i_h['EKG-evt'] >= i_i[0].iloc[-1]]) / 30) \
for i_h, i_i in zip(Hanimals, Ianimals)])

关于python - 使用字符串通过 "for"语句压缩重复代码 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39148047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com