gpt4 book ai didi

python - 将 for 循环的输出写入 pandas 数据帧

转载 作者:行者123 更新时间:2023-12-02 19:46:21 25 4
gpt4 key购买 nike

如何将 for 循环的输出写入 pandas 数据帧?

输入数据是数据帧 (df_elements) 列表。

[                          seq  score    status
1652 TGGCTTCGATTTTGTTATCGATG -0.22 negative
1277 GTACTGTGGAATCTCGGCAGGCT 4.87 negative
302 CCAAAGTCTCACTTGTTGAGAAC -4.66 negative
1756 TGGCGGTGGTGGCGGCGCAGAGC 1.55 negative
5043 TGACGAAACATCTTATAAAGGAA 1.96 negative
3859 CAGAGCTCTTCAAACTTAAGAAC -0.39 negative
1937 GTATGCTTGTGCTTCTCCAAAAA -0.91 negative
2805 GGCCGGCCTGTGGTCGACGGGGA -3.26 negative
3353 CCGATGGGC -1.97 negative
5352 ACTTACTATTTACTGATCAGCAC 3.53 negative
5901 TTGAGGCTCTCCTTATCCAGATT 6.37 negative
5790 AAGGAAACGTGTAATGATAGGCG -2.69 negative, seq score status
2197 CTTCCATTGAGCTGCTCCAGCAC -0.97 negative
1336 CCAAATGCAACAATTCAAAGCCC -0.44 negative
4825 CAATTTTGT -6.44 negative
4991 ATACTGTTTGCTCACAAAAGGAG 2.15 negative
1652 TGGCTTCGATTTTGTTATCGATG -0.22 negative
1964 ACCACTTTGTGGACGAATACGAC -4.51 negative
4443 TTCCTCGTCTAGCCTTTCAGTGC 3.05 negative
4208 TGGCTGTGAACCCCTATCAGCTG 2.70 negative
212 CTGTCGTTTCAATGTTTAAGATA 6.43 negative
775 GCTTTAAGT 0.06 negative
3899 GAGCAAAGC -6.61 negative

我正在尝试将以下 for 循环的输出写入数据帧。我尝试创建一个空列表(数据)并使用 data.append 附加逐行输出。我收到类似 cannot concatenate object of type "";

的错误

下面给出的代码在控制台中打印输出:


cut_off = [0,1,2]

for co in cut_off:
for df in df_elements:
print co, "\t", str((df['score'] > co).sum())

代码应将 cut_off 值与列得分进行比较,并打印每个数据帧元素的总计,其中得分 > 于 cut_off。

输出应如下所示:

cutoff number
0 5 #for first dataframe element
0 5 #for second dataframe element

最佳答案

# create empty lists for cutoff and number
cutoff_list = []
number_list = []

# loop through cutoff values and dataframes, to populate your lists
for co in cut_off:
for df in df_elements:
cutoff_list.append(co)
number_list.append((df['score'] > co).sum())

# create dataframe from your lists
df = pd.DataFrame(list(zip(cutoff_list , number_list)),
columns =['cutoff', 'number'])

# get your desired output
print(df)

关于python - 将 for 循环的输出写入 pandas 数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59174436/

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