循环后生成的list列表

问题描述 投票:0回答:0

我创建了两个空数据框:

df1 = pd.DataFrame(columns=['Iteration','R'])
df2 = pd.DataFrame(columns=['Iteration','C'])

和两个列表

my_list1 = []
my_list2 = []

然后我的代码进入了一个有条件的 for 循环。 (之前定义了名为“d”的数据框)

count = 0

for i in range(1000):
    # create new s DataFrame for each iteration
    s = pd.DataFrame(np.random.lognormal(mu, sigma, size=(1000, 1)))

    if i == 0:
        df2 = pd.DataFrame(columns=['Iteration','R'])
        df1 = pd.DataFrame(columns=['Iteration','C'])

    my_list1= []
    my_list2= []

    for col in s.columns:
        # loop through each row of d and s
        for idx, row in enumerate(stock.values):
            # check if d is within 5% range of s value
            if (row >= 0.95*s.iloc[idx, col]) and (row <= 1.05*s.iloc[idx, col]):
                # update d and my_list2 with 0
                d.iloc[idx,col] = 0
                my_list2.append([i,0])
                my_list1.append([i,d.iloc[idx, col]])
            # check if d is greater than s value
            elif row > s.iloc[idx, col]:
                # calculate difference and update d, my_list2, and my_list1
                diff = row - s.iloc[idx, col]
                d.iloc[idx,col] = diff
                my_list2.append([i,diff])
                my_list1.append([i,s.iloc[idx, col]])
            # d is less than s value
            else:
                # update my_list2 with d value
                d.iloc[idx,col] = row
                my_list2.append([i,row])
                my_list1.append([i,0])
                count+=1

    # concatenate d into single dataframe
    result = pd.concat([d], axis=1)
    df2 = pd.concat([df2, pd.DataFrame(my_list2, columns=['Iteration','R'])], axis=0)
    df1 = pd.concat([df1, pd.DataFrame(my_list1, columns=['Iteration','C'])], axis=0)
After it I would like to add the results within dataframes by using pd.concat.


    df1 = pd.concat([df1, pd.DataFrame(my_list1, columns=['Iteration','R'])], axis=0)
    df2 = pd.concat([df2, pd.DataFrame(my_list2, columns=['Iteration','C'])], axis=0)

我不知道为什么,但是当我打印 df1 数据时,它是这样的:

0           0           2.56
1           0           5.78
2           0           125.23
3           0           9.56
4           0           12.54
..        ...         ...
995       999           0
996       999           0
997       999           0
998       999           0
999       999           0

而来自 df2 的数据看起来像这样:

0          999                  [0.0]
1          999                  [0.0]
2          999                  [0.0]
3          999  [0.32658459858058686]
4          999                  [0.0]
..         ...                    ...
995        999                  [0.0]
996        999                  [0.0]
997        999                  [0.0]
998        999  [0.21416404721485155]
999        999  [0.07233033046780268]

如果我打印 my_list2 它看起来像这样:

[[999, array([0.])],
[999, array([0.])],
[999, array([0.])],
[999, array([0.3265846])],
[999, array([0.])],
[999, array([0.15574123])],
[999, array([0.0840484])],
[999, array([0.])],
[999, array([0.02607298])],
[999, array([0.02204838])],
[999, array([0.44597774])],
[999, array([0.5177778])]]

我想这是一个列表列表。我如何使用 pd.concat 来获得具有两列(Iteration 和 R)以及 df1 的 DataFrame?

python arrays dataframe list for-loop
© www.soinside.com 2019 - 2024. All rights reserved.