2017-09-03 131 views
2
無索引,漂亮,列

我經常發現自己寫的代碼像這樣得到很好地格式化,以多列輸出(無指標)在大熊貓調試或學習我的數據時:內置,輸出與熊貓

dfs = dfs[dfs['some_id'] == the_id] 
    cols = [ 
     'some_col', 
     'another_col', 
     'yet_another', 
    ] 

    print("\t".join(cols)) 
    for row in dfs[cols].values: 
     print("\t\t".join([str(val) for val in row])) 

這工作正常,但我想知道是否有內置的方式來獲得這種輸出與熊貓功能或直接查找語法。輸出示例:

some_col another_col yet_another 
a   b    c 
x   y    z 

回答

2

是的,你可以調用df.to_string與參數index=False

dfs = dfs[dfs['some_id'] == the_id] 
    cols = [ 
     'some_col', 
     'another_col', 
     'yet_another', 
    ] 

print(dfs[cols].to_string(index=False)) 

MCVE:

print(df) 

      0   1 
0 0.335232 -1.256177 
1 -1.367855 0.746646 
2 0.027753 -1.176076 
3 0.230930 -0.679613 
4 1.261967 0.570967 

print(df.to_string(index=False, col_space=10)) 

0   1 
0.335232 -1.256177 
-1.367855 0.746646 
0.027753 -1.176076 
0.230930 -0.679613 
1.261967 0.570967 
+0

我通常用它來查看所有或單列。我如何使用它來選擇總列的子集? –

+1

@JB在頂部添加了對特定代碼的編輯。 –