从in64数据类型中获取不同的对象返回

时间:2016-12-21 16:23:04

标签: python pandas format

作为一个大功能的一部分,我坚持使用最后一行。 我应该得到(pandas)数据帧中最高(很少)值的行和列名称。因此,我将其拆开,对其进行分类并打印最后两行。使用:

df2=zerotriangle_frame.unstack()
sorted_df = df2.sort_values(inplace=True)
x = df2[-2:]

这导致:

seq_6120  seq_1761    34
seq_4833  seq_1761    37
dtype: int64

这很好(分别是:seq_4833和seq_1761是类似于最高值的行和列名称(上下文中的最高序列同义),seq_6120和seq_1761类似于第二个最高值...)但我想以某种方式格式化此输出这样我就可以在打印部分使用不同的部分。例如:

print("sequenc {0} and sequence {1} got the highest simmilarity value:{2}".format(a, b, c))

其中a,b和c分别为seq_4833,seq_1761和37 ...... 如果结果是一个列表或元组我可以拆分它,但现在我被卡住了。

PS:此外,当值(示例中的34和37)相同时,我应该打印一些特定的错误消息(例如'超过1对具有相同同义性的序列')。

1 个答案:

答案 0 :(得分:0)

假设df2是你的最终数据帧,那么你可以只过滤掉所有等于最大值的行,然后写一个if语句来产生输出。

# assuming df2 is a dataframe with columns a,b,c
df3 = df2[df2.c == df2.c.max()]

if len(df3) == 1:
    print("sequence {0} and sequence {1} got the highest simmilarity value:{2}".format(df3.a.values[0], df3.b.values[0], df3.c.values[0]))
else:
    for i, row in df3.iterrows():
        print("sequence {0} and sequence {1} got the same simmilarity value:{2}".format(row.a, row.b, row.c))