使用不同的变量以相同的速率迭代循环

时间:2017-07-25 15:40:07

标签: python pandas

我试图找出如何同时迭代一个循环。不确定我是否正确地写了这个问题。

例如,我需要将两个数据帧的第1列的差异存储在df的Calibrated Power 0 [dBm]或第4列中。我需要将第2列存储在Calibrated Power 10 [dBm]列中。目前,相同的数据正被复制到df的第4-6列

attenuator_power = [0, 10, 30]
for i in range(1, 3):
    ydata_sans_att = df_sans_attenuator.iloc[:, i]
    ydata_df = df.iloc[:, i]
    for j in attenuator_power:
        df['Calibrated Power {} [dBm]'.format(j)] = ydata_df - ydata_sans_att

2 个答案:

答案 0 :(得分:1)

这就是你配对的方式:

for i, j in zip(range(1,3), attenuator_power):
    ydata_sans_att = df_sans_attenuator.iloc[:, i]
    ydata_df = df.iloc[:, i]
    df['Calibrated Power {} [dBm]'.format(j)] = ydata_df - ydata_sans_att  

答案 1 :(得分:1)

@mauve已经给出了一个很好的答案,但这是另一种选择:

for i, j in enumerate(attenuator_power, 1):
    ydata_sans_att = df_sans_attenuator.iloc[:, i]
    ydata_df = df.iloc[:, i]
    df['Calibrated Power {} [dBm]'.format(j)] = ydata_df - ydata_sans_att