[英]Writing output of a for loop to pandas data-frame
How can I write an output of a for loop to pandas data-frame?如何将 for 循环的输出写入 Pandas 数据框?
Input data is a list of data-frames (df_elements).输入数据是一个数据框列表(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
I am trying to write the output of the below for loop to a data-frame.我正在尝试将以下 for 循环的输出写入数据帧。 I tried by creating an empty list (data) and append row-wise output using data.append.
我尝试通过创建一个空列表(数据)并使用 data.append 附加行输出。 I am getting an error like cannot concatenate object of type "";
我收到一个错误,例如无法连接“”类型的对象;
The code is given below which print the output in the console:下面给出了在控制台中打印输出的代码:
cut_off = [0,1,2]
for co in cut_off:
for df in df_elements:
print co, "\t", str((df['score'] > co).sum())
The code should compare the cut_off value to the column score and print the total for each data-frame element, where the score is > than cut_off.代码应该将 cut_off 值与列分数进行比较,并打印每个数据框元素的总数,其中分数大于 cut_off。
The output should look like this:输出应如下所示:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.