pandas 打印没有省略号的 numpy 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44311664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:43:06  来源:igfitidea点击:

Print numpy array without ellipsis

pythonarrayspandasnumpy

提问by Harjatin

I want to print a numpy array without truncation. I have seen other solutions but those don't seem to work.

我想打印一个不截断的 numpy 数组。我见过其他解决方案,但这些解决方案似乎不起作用。

Here is the code snippet:

这是代码片段:

total_list = np.array(total_list)
np.set_printoptions(threshold=np.inf)
print(total_list)

And this is what the output looks like:

这就是输出的样子:

22        A
23        G
24        C
25        T
26        A
27        A
28        A
29        G
         ..
232272    G
232273    T
232274    G
232275    C
232276    T
232277    C
232278    G
232279    T

This is the entire code. I might be making a mistake in type casting.

这是整个代码。我可能在类型转换上犯了一个错误。

import csv
import pandas as pd
import numpy as np



seqs = pd.read_csv('BAP_GBS_BTXv2_imp801.hmp.csv')
plts = pd.read_csv('BAP16_PlotPlan.csv')

required_rows = np.array([7,11,14,19,22,31,35,47,50,55,58,63,66,72,74,79,82,87,90,93,99])
total_list = []


for i in range(len(required_rows)):
    curr_row = required_rows[i];
    print(curr_row)
    for j in range(len(plts.RW)):
        if(curr_row == plts.RW[j]):
            curr_plt = plts.PI[j]
            curr_range = plts.RA1[j]
            curr_plt = curr_plt.replace("_", "").lower()
            if curr_plt in seqs.columns:
                new_item = [curr_row,curr_range,seqs[curr_plt]]
                total_list.append(new_item)
                print(seqs[curr_plt]) 


total_list = np.array(total_list)
'''
np.savetxt("foo.csv", total_list[:,2], delimiter=',',fmt='%s')
total_list[:,2].tofile('seqs.csv',sep=',',format='%s')
'''
np.set_printoptions(threshold='nan')

print(total_list)

回答by Szabolcs Dombi

use the following snippet to get no ellipsis.

使用以下代码段不会出现省略号。

import numpy
import sys
numpy.set_printoptions(threshold=sys.maxsize)

EDIT:

编辑:

If you have a pandas.DataFrameuse the following snippet to print your array:

如果您有pandas.DataFrame使用以下代码段来打印您的数组:

def print_full(x):
    pd.set_option('display.max_rows', len(x))
    print(x)
    pd.reset_option('display.max_rows')

Or you can use the pandas.DataFrame.to_string()method to get the desired result.

或者您可以使用pandas.DataFrame.to_string()方法来获得所需的结果。

EDIT':

编辑':

An earlier version of this post suggested the option below

这篇文章的早期版本建议了以下选项

numpy.set_printoptions(threshold='nan')

Technically, this might work, however, the numpy documentation specifies int and None as allowed types. Reference: https://docs.scipy.org/doc/numpy/reference/generated/numpy.set_printoptions.html.

从技术上讲,这可能有效,但是,numpy 文档将 int 和 None 指定为允许的类型。参考:https: //docs.scipy.org/doc/numpy/reference/generated/numpy.set_printoptions.html

回答by a p

You can get around the weird Numpy repr/print behavior by changing it to a list:

您可以通过将其更改为 a 来解决奇怪的 Numpy repr/print 行为list

print list(total_list)

should print out your list of 2-element np arrays.

应该打印出您的 2 元素 np 数组列表。

回答by Szabolcs Dombi

You are notprinting numpy arrays.

不是在打印 numpy 数组。

Add the following line after the imports:

在导入后添加以下行:

pd.set_option('display.max_rows', 100000)

回答by GrigoreG

#for a 2d array
def print_full(x):
    dim = x.shape
    pd.set_option('display.max_rows', dim[0])#dim[0] = len(x)
    pd.set_option('display.max_columns', dim[1])
    print(x)
    pd.reset_option('display.max_rows')
    pd.reset_option('display.max_columns')

回答by simonalexander2005

It appears that as of Python 3, the threshold can no longer be unlimited.

从 Python 3 开始,阈值似乎不再是无限的。

Therefore, the recommended option is:

因此,推荐的选项是:

import numpy
import sys
numpy.set_printoptions(threshold=sys.maxsize)