简体   繁体   English

python 中循环的正确语法

[英]Proper syntax of a loop in python

I am new in python and I would like to see the solution from experts on the below, as choosing values from some dataframe based on values from some other one, is something that I am using often.我是 python 的新手,我想从下面的专家那里看到解决方案,因为根据其他值从一些 dataframe 中选择值,这是我经常使用的东西。

I have the first df below.我有下面的第一个df。

data = {'Date': ['31/03/1947', '30/06/1947', '30/09/1947', '31/12/1947', '31/03/1948', '30/06/1948', '30/09/1948', '31/12/1948', '31/03/1949', '30/06/1949'],
        'Italian GDP': [-0.057750, -0.054695, -0.052334, -0.051142, -0.050267, -0.049659, -0.048984, -0.048839, -0.046507, -0.045941],
        'US GDP': [-0.011017, -0.008948, -0.007276, -0.006526, -0.005046, -0.002011, -0.001592, -0.000720, 0.000085, 0.000334],
        'EA GDP': [0.009119, 0.010925, 0.011530, 0.014639, 0.015634, 0.016439, 0.018998, 0.025592, 0.032806, 0.035710],
        'FR GDP': [-0.011773, -0.010264, -0.009310, -0.009126, -0.006450, -0.005746, -0.004998, -0.004780, 0.001206, 0.004616],
        'DE GDP': [-0.030926, -0.023653, -0.023418, -0.021585, -0.010145, -0.006971, -0.005937, -0.005850, -0.005215, -0.000496],
        'CZ GDP': [-0.017287, -0.013185, -0.011872, -0.006948, -0.004398, -0.004028, -0.001028, -0.000814, 0.000349, 0.001409],
        'HU GDP': [-0.068923, -0.060180, -0.058478, -0.057246, -0.053871, -0.053105, -0.052404, -0.052222, -0.050352, -0.049721],
        'UK GDP': [-0.029143, -0.028303, -0.027973, -0.027784, -0.025187, -0.024922, -0.024092, -0.022788, -0.022478, -0.021911],
        'NL GDP': [-0.011381, -0.010251, -0.009614, -0.008191, -0.007078, -0.006006, -0.005573, -0.000319, -0.000199, 0.000804],
        'RO GDP': [-0.082121, -0.076776, -0.074611, -0.073923, -0.070768, -0.060687, -0.060526, -0.054234, -0.047550, -0.032161]}

# read in with 
df = pd.DataFrame(data).set_index('Date')

and a second df below:和下面的第二个df:

Values = {'GDPs': ['Italian GDP', 'US GDP', 'EA GDP', 'FR GDP', 'DE GDP', 'CZ GDP', 'HU GDP', 'UK GDP', 'NL GDP', 'RO GDP'],
        'Observations': [89, 281, 89, 169, 105, 85, 89, 169, 85, 89],
        'Round Down': [1.0, 5.0, 1.0, 3.0, 2.0, 1.0, 1.0, 3.0, 1.0, 1.0],
        'Round Up': [2.0, 6.0, 2.0, 4.0, 3.0, 2.0, 2.0, 4.0, 2.0, 2.0]}
# read in with 
roundposnew = pd.DataFrame(Values).set_index('GDPs')

For the second df, I would like to add two additional columns that would give me the corresponding "Round Up" and "Round Down" values of the first df.对于第二个 df,我想添加两个额外的列,它们将为我提供第一个 df 的相应“向上取整”和“向下取整”值。 For example, I would like to get the first value and second value of the "Italian GDP", the 5th and 6th value of the "US GDP" etc. I have written the code below just for the "Round Down" values (one of the columns i want to add) but is not working, it gives just the result for last loop value.例如,我想得到“Italian GDP”的第一个值和第二个值,“US GDP”的第 5 个和第 6 个值等。我编写了下面的代码只是为了“Round Down”值(一个我想添加的列)但不工作,它只给出最后一个循环值的结果。 Can you please advise what would be the proper way of doing it?你能告诉我正确的做法是什么吗? Thank you in advance!先感谢您!

for i in df.columns:
    fal = df.columns.get_loc(i)
    ld5 = df.iloc[int(roundposnew.loc[i,'Round Down']-1),int(fal)]
ld5

>>> -0.08212129722076356
  • When roundposnew is created, Round Down and Round UP should be int , not float (eg 1 vs. 1.0 ) because the value is being used to index another dataframe.创建roundposnew时, Round DownRound UP应该是int ,而不是float (例如11.0 ),因为该值用于索引另一个 dataframe。
    • Then there's no reason for converting to int in the loop那么就没有理由在循环中转换为int
  • Write the values to a list and then create the new column将值写入列表,然后创建新列
    • Dataframe columns should be created all at once with a list or array of values. Dataframe 列应使用值列表或数组一次创建。
    • Trying to create a column in a loop, as your were probably doing, repeatedly assigns each value from the loop, to all the rows in the column.尝试在循环中创建一列,就像您可能正在做的那样,重复地将循环中的每个值分配给列中的所有行。
  • As a note, the logic of what you're doing, manually selecting these particular values, doesn't make sense.请注意,您正在做的事情的逻辑,手动选择这些特定值,没有意义。 It seems like there is something needing a programmatic implementation.似乎有些东西需要程序化实现。
    • What is the logic behind selecting the values from df to be added to roundposnew ?df中选择要添加到roundposnew的值背后的逻辑是什么? Why those specific rows, for each column?为什么每一列都有这些特定的行?
import pandas as pd

Values = {'GDPs': ['Italian GDP', 'US GDP', 'EA GDP', 'FR GDP', 'DE GDP', 'CZ GDP', 'HU GDP', 'UK GDP', 'NL GDP', 'RO GDP'],
          'Observations': [89, 281, 89, 169, 105, 85, 89, 169, 85, 89],
          'Round Down': [1, 5, 1, 3, 2, 1, 1, 3, 1, 1],
          'Round Up': [2, 6, 2, 4, 3, 2, 2, 4, 2, 2]}

# read in with 
roundposnew = pd.DataFrame(Values).set_index('GDPs')

# round up and down list
ru = list()
rd = list()

# loop to add values to lists
for i in df.columns:
    fal = df.columns.get_loc(i)  # this is an int, doesn't need int(fal)
    rd.append(df.iloc[roundposnew.loc[i,'Round Down'] - 1, fal])
    ru.append(df.iloc[roundposnew.loc[i, 'Round Up'] + 1, fal])

# add lists to dataframe
roundposnew['rd'] = rd
roundposnew['ru'] = ru

             Observations  Round Down  Round Up        rd        ru
GDPs                                                               
Italian GDP            89           1         2 -0.057750 -0.051142
US GDP                281           5         6 -0.005046 -0.000720
EA GDP                 89           1         2  0.009119  0.014639
FR GDP                169           3         4 -0.009310 -0.005746
DE GDP                105           2         3 -0.023653 -0.010145
CZ GDP                 85           1         2 -0.017287 -0.006948
HU GDP                 89           1         2 -0.068923 -0.057246
UK GDP                169           3         4 -0.027973 -0.024922
NL GDP                 85           1         2 -0.011381 -0.008191
RO GDP                 89           1         2 -0.082121 -0.073923

I assume you want to add 2 additional columns, you could do it like this: first you add 2 columns filled with 0 values (or whatever you want) and later you update these values with your rounddown/roundup formula我假设您要添加 2 个额外的列,您可以这样做:首先添加 2 个填充有 0 值(或任何您想要的值)的列,然后使用您的舍入/舍入公式更新这些值

roundposnew['Rounded up'] = 0.
roundposnew['Rounded down'] = 0.
for i in df.columns:
    fal = df.columns.get_loc(i)
    ld5 = df.iloc[int(roundposnew.loc[i,'Round Down']-1),int(fal)]
    ld6 = df.iloc[int(roundposnew.loc[i, 'Round Up'] + 1), int(fal)]
    roundposnew.loc[i,'Rounded down'] = ld5
    roundposnew.loc[i, 'Rounded up'] = ld6
    print('values for '+i+' are: \n rounded down: '+str(ld5)+' \n rounded up: '+str(ld6))
print(roundposnew)

Output looks like: Output 看起来像:

             Observations  Round Down  Round Up  Rounded up  Rounded down
GDPs                                                                     
Italian GDP            89         1.0       2.0   -0.051142     -0.057750
US GDP                281         5.0       6.0   -0.000720     -0.005046
EA GDP                 89         1.0       2.0    0.014639      0.009119
FR GDP                169         3.0       4.0   -0.005746     -0.009310
DE GDP                105         2.0       3.0   -0.010145     -0.023653
CZ GDP                 85         1.0       2.0   -0.006948     -0.017287
HU GDP                 89         1.0       2.0   -0.057246     -0.068923
UK GDP                169         3.0       4.0   -0.024922     -0.027973
NL GDP                 85         1.0       2.0   -0.008191     -0.011381
RO GDP                 89         1.0       2.0   -0.073923     -0.082121

Hope this shows you a way to solve your issue!希望这向您展示了解决问题的方法!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM