pandas for循环在列表中追加,但输入是 Dataframe

yvgpqqbh  于 2022-11-20  发布在  其他
关注(0)|答案(1)|浏览(159)

下面我有一段Python代码。只是一个例子来说明这个问题:我想根据一些值在数据框中选择一些行。不知何故,这个需要在一个for循环中,我使用了.append()来将每一行的选择添加到最终文件中。但是结果和我预期的不一样。我通过阅读相当多的帖子了解到,我们不应该在循环中作为 Dataframe 进行追加。所以我现在不知道如何才能做到这一点。有人能帮忙吗?非常感谢!

import pandas as pd

df = pd.DataFrame({'a': [4, 5, 6, 7], 'b': [10, 20, 30, 40], 'c': [100, 50, -30, -50]})
df['diff'] = (df['b'] - df['c']).abs()
print(df)
df1 = df[df['diff'] == 90]
df2 = df[df['diff'] == 60]

list = [df1, df2]

def try_1(list):
    output = []
    for item in list:
        output.append(item)
    return output

print(try_1(list))

代码输出

a   b    c  diff
0  4  10  100    90
1  5  20   50    30
2  6  30  -30    60
3  7  40  -50    90

[   a   b    c  diff
0  4  10  100    90
3  7  40  -50    90,    a   b   c  diff
2  6  30 -30    60]

但是print(try_1(list))的期望输出

a   b    c  diff
4  10  100    90
7  40  -50    90
6  30  -30    60

还有,我需要把最后一个写进一个文件。我试过了。write(),它抱怨不是一个字符串。我该怎么解决这个问题呢?谢谢!

kb5ga3dv

kb5ga3dv1#

你的代码只是重新创建了你之前拥有的相同列表,你可以只使用pd.concat来代替,要将它写入一个帧,你必须先将它转换为str

import pandas as pd

df = pd.DataFrame({'a': [4, 5, 6, 7], 'b': [10, 20, 30, 40], 'c': [100, 50, -30, -50]})
df['diff'] = (df['b'] - df['c']).abs()
# print(df)
df1 = df[df['diff'] == 90]
df2 = df[df['diff'] == 60]

my_list = [df1, df2]

all_frames = pd.concat(my_list)
with open("file", "w") as f:
    f.write(str(all_frames))

如果你需要在for循环中追加,偶尔写上,你可以这样做:

import pandas as pd

df = pd.DataFrame({'a': [4, 5, 6, 7], 'b': [10, 20, 30, 40], 'c': [100, 50, -30, -50]})
df['diff'] = (df['b'] - df['c']).abs()
# print(df)
df1 = df[df['diff'] == 90]
df2 = df[df['diff'] == 60]

my_list = [df1, df2]
for i in range(20):
    my_list.append(df2)
    if i % 5 == 0: # whenever we want to write
        all_frames = pd.concat(my_list)
        my_list = [all_frames]
        with open("file", "w") as f:
            f.write(str(all_frames))

相关问题