我正在使用NBA Draft数据框,该数据框可以从here获取并通过熊猫读取。
当我尝试将csv加载到DataFrame时,列会被切片吗?使用以下代码:
import pandas as pd
r = pd.DataFrame.from_csv('data/historical_projections.csv')
print r
结果:
Position ID Draft Year \
Player
Karl-Anthony Towns C karl-anthony-towns 2015
Justise Winslow SF justise-winslow 2015
Stanley Johnson SF stanley-johnson 2015
Jahlil Okafor C jahlil-okafor 2015
D`Angelo Russell PG d-angelo-russell 2015
Dakari Johnson C dakari-johnson 2015
Devin Booker SG 3-devin-booker 2015
Willie Cauley-Stein C willie-cauley-stein 2015
Rondae Hollis-Jefferson SF rondae-hollis-jefferson 2015
Trey Lyles PF trey-lyles 2015
Sam Dekker SF sam-dekker 2015
Kelly Oubre Jr. SF kelly-oubre-jr 2015
Tyus Jones PG tyus-jones 2015
Kevon Looney PF kevon-looney 2015
Myles Turner PF 2-myles-turner 2015
Cliff Alexander PF cliff-alexander 2015
Christian Wood PF christian-wood 2015
Frank Kaminsky C frank-kaminsky 2015
Michael Frazier II SG michael-frazier-ii 2015
Brandon Ashley PF brandon-ashley 2015
Bobby Portis PF bobby-portis 2015
Andrew Harrison PG andrew-harrison 2015
Rashad Vaughn SG rashad-vaughn 2015
Aaron Harrison SG 3-aaron-harrison 2015
Montrezl Harrell PF montrezl-harrell 2015
Quinn Cook PG quinn-cook 2015
Cameron Payne PG 2-cameron-payne 2015
Chris McCullough PF chris-mccullough 2015
Justin Anderson SF justin-anderson 2015
Terry Rozier PG terry-rozier 2015
... ... ... ...
Jeff Trepagnier SG jeff-trepagnier 2001
Jeryl Sasser PG jeryl-sasser 2001
Terence Morris SF terence-morris 2001
SirValiant Brown PG sirvaliant-brown 2001
Michael Wright PF michael-wright 2001
Maurice Evans SG maurice-evans 2001
Alvin Jones C alvin-jones 2001
Jerry Green SG jerry-green 2001
Kenny Gregory SF kenny-gregory 2001
Casey Calvary PF casey-calvary 2001
Lee Scruggs SF lee-scruggs 2001
Rashad Phillips PG rashad-phillips 2001
Brian Scalabrine PF brian-scalabrine 2001
Kirk Haston PF kirk-haston 2001
Kimani Ffriend C kimani-ffriend 2001
Damone Brown SF damone-brown 2001
Darren Kelly SG darren-kelly 2001
Michael Hicks SG 11-michael-hicks 2001
Will Solomon PG will-solomon 2001
Monty Mack SG monty-mack 2001
Dean Oliver PG dean-oliver 2001
Sean Lampley SF sean-lampley 2001
Terry Black SF terry-black 2001
Cookie Belcher PG cookie-belcher 2001
Eric Chenowith C eric-chenowith 2001
Terrell Lyday SG terrell-lyday 2001
Brandon Wolfram SF brandon-wolfram 2001
Ruben Boumtje Boumtje C ruben-boumtje-boumtje 2001
Greg Stevenson SG greg-stevenson 2001
Calvin Bowman C calvin-bowman 2001
Projected SPM Superstar Starter Role Player \
Player
Karl-Anthony Towns 1.030606 0.134767 0.427183 0.163083
Justise Winslow 0.875329 0.083529 0.510905 0.176767
Stanley Johnson 0.679493 0.067800 0.423733 0.278500
Jahlil Okafor 0.521661 0.058717 0.409900 0.235533
D`Angelo Russell 0.511967 0.152033 0.342283 0.096583
Dakari Johnson 0.491787 0.021341 0.367544 0.417572
Devin Booker 0.472577 0.073367 0.324467 0.390167
Willie Cauley-Stein 0.351171 0.047114 0.405990 0.243186
Rondae Hollis-Jefferson 0.311909 0.014590 0.368533 0.392481
Trey Lyles 0.267509 0.022386 0.351329 0.403052
Sam Dekker 0.254165 0.008692 0.385004 0.329054
Kelly Oubre Jr. 0.247310 0.019833 0.378983 0.337967
Tyus Jones 0.243549 0.027200 0.414562 0.284405
Kevon Looney 0.204173 0.008300 0.355567 0.374917
Myles Turner 0.076381 0.029417 0.335798 0.318517
Cliff Alexander 0.026134 0.007633 0.294783 0.406617
Christian Wood -0.019561 0.008397 0.311111 0.393688
Frank Kaminsky -0.072352 0.006718 0.352835 0.327164
Michael Frazier II -0.167051 0.010084 0.254307 0.381989
Brandon Ashley -0.170210 0.011187 0.253265 0.356139
Bobby Portis -0.314574 0.003141 0.253027 0.386533
Andrew Harrison -0.337041 0.011228 0.209973 0.393991
Rashad Vaughn -0.342326 0.020667 0.242333 0.367800
Aaron Harrison -0.420844 0.009669 0.191223 0.391380
Montrezl Harrell -0.436183 0.003904 0.200321 0.411584
Quinn Cook -0.453518 0.000128 0.221395 0.365234
Cameron Payne -0.604345 0.009530 0.190084 0.316888
Chris McCullough -0.630996 0.006567 0.192133 0.293200
Justin Anderson -0.635745 0.000926 0.157187 0.412167
Terry Rozier -0.637634 0.007467 0.188248 0.299670
... ... ... ... ...
Jeff Trepagnier -0.911776 0.003727 0.097090 0.380866
Jeryl Sasser -0.915548 0.001558 0.056811 0.528500
Terence Morris -0.936639 0.001284 0.106538 0.380679
SirValiant Brown -0.951670 0.004985 0.102306 0.349285
Michael Wright -1.051887 0.004342 0.074562 0.367175
Maurice Evans -1.065428 0.001411 0.060288 0.409847
Alvin Jones -1.088554 0.000694 0.062898 0.382907
Jerry Green -1.103679 0.004745 0.045049 0.393803
Kenny Gregory -1.105271 0.000606 0.061600 0.389261
Casey Calvary -1.108822 0.001052 0.058460 0.391861
Lee Scruggs -1.163638 0.000261 0.042095 0.358225
Rashad Phillips -1.230746 0.001420 0.031098 0.354066
Brian Scalabrine -1.245361 0.000000 0.017496 0.402688
Kirk Haston -1.251493 0.002086 0.039629 0.327627
Kimani Ffriend -1.257651 0.000810 0.060046 0.304163
Damone Brown -1.259393 0.000000 0.043803 0.335953
Darren Kelly -1.273854 0.000000 0.040028 0.339174
Michael Hicks -1.301079 0.000338 0.018563 0.356263
Will Solomon -1.301901 0.000000 0.025063 0.342152
Monty Mack -1.348996 0.000140 0.016068 0.352727
Dean Oliver -1.359015 0.000145 0.028928 0.309891
Sean Lampley -1.379912 0.000797 0.018403 0.308882
Terry Black -1.398119 0.000680 0.022434 0.291499
Cookie Belcher -1.453962 0.003407 0.038351 0.227702
Eric Chenowith -1.462131 0.000548 0.030222 0.236204
Terrell Lyday -1.486319 0.001152 0.018303 0.261035
Brandon Wolfram -1.493460 0.000271 0.011532 0.271560
Ruben Boumtje Boumtje -1.494069 0.000000 0.020653 0.236919
Greg Stevenson -1.529188 0.000000 0.019693 0.238861
Calvin Bowman -1.684669 0.000000 0.005068 0.168629
Bust
Player
Karl-Anthony Towns 0.274967
Justise Winslow 0.228800
Stanley Johnson 0.229967
Jahlil Okafor 0.295850
D`Angelo Russell 0.409100
Dakari Johnson 0.193544
Devin Booker 0.212000
Willie Cauley-Stein 0.303710
Rondae Hollis-Jefferson 0.224396
Trey Lyles 0.223233
Sam Dekker 0.277250
Kelly Oubre Jr. 0.263217
Tyus Jones 0.273833
Kevon Looney 0.261217
Myles Turner 0.316269
Cliff Alexander 0.290967
Christian Wood 0.286804
Frank Kaminsky 0.313283
Michael Frazier II 0.353620
Brandon Ashley 0.379410
Bobby Portis 0.357298
Andrew Harrison 0.384809
Rashad Vaughn 0.369200
Aaron Harrison 0.407728
Montrezl Harrell 0.384191
Quinn Cook 0.413243
Cameron Payne 0.483498
Chris McCullough 0.508100
Justin Anderson 0.429720
Terry Rozier 0.504615
... ...
Jeff Trepagnier 0.518318
Jeryl Sasser 0.413132
Terence Morris 0.511499
SirValiant Brown 0.543423
Michael Wright 0.553921
Maurice Evans 0.528454
Alvin Jones 0.553500
Jerry Green 0.556404
Kenny Gregory 0.548532
Casey Calvary 0.548627
Lee Scruggs 0.599418
Rashad Phillips 0.613415
Brian Scalabrine 0.579816
Kirk Haston 0.630658
Kimani Ffriend 0.634980
Damone Brown 0.620244
Darren Kelly 0.620798
Michael Hicks 0.624837
Will Solomon 0.632785
Monty Mack 0.631065
Dean Oliver 0.661035
Sean Lampley 0.671918
Terry Black 0.685388
Cookie Belcher 0.730541
Eric Chenowith 0.733027
Terrell Lyday 0.719510
Brandon Wolfram 0.716636
Ruben Boumtje Boumtje 0.742427
Greg Stevenson 0.741446
Calvin Bowman 0.826303
所以我尝试添加index_col=[0,1,2,3,4,5,6,7]
,但仍然没有得到想要的内容(您可以看到右侧的Bust带有空格):
Bust
Player Position ID Draft Year Projected SPM Superstar Starter Role Player
Karl-Anthony Towns C karl-anthony-towns 2015-01-01 1.030606 0.134767 0.427183 0.163083 0.274967
Justise Winslow SF justise-winslow 2015-01-01 0.875329 0.083529 0.510905 0.176767 0.228800
Stanley Johnson SF stanley-johnson 2015-01-01 0.679493 0.067800 0.423733 0.278500 0.229967
Jahlil Okafor C jahlil-okafor 2015-01-01 0.521661 0.058717 0.409900 0.235533 0.295850
D`Angelo Russell PG d-angelo-russell 2015-01-01 0.511967 0.152033 0.342283 0.096583 0.409100
所以..有没有办法(我无法让行列工作)将DataFrame呈现得尽可能干净?谢谢。
答案 0 :(得分:0)
您想在屏幕上看到整个数据框吗?看看四舍五入是否适合您的需求
r.round(2)
答案 1 :(得分:0)
为了减小数据帧输出的宽度,您可以尝试以下操作:
首先,按照this的答案建议对浮点数进行四舍五入。为此,您可以执行以下操作:
import pandas as pd
r = pd.DataFrame.from_csv('data/historical_projections.csv')
r = r.round(2)
print r
或者:
import pandas as pd
r = pd.DataFrame.from_csv('data/historical_projections.csv')
print r.round(2)
但是请注意,第二个选项不会保留您在r
中所做的更改,而只会保留您的 print 舍入后的数字
如果这还不够,您可以使用.columns
减少列名的长度:
r.columns = ['Pos', 'ID', 'Draft Year', 'Proj SPM', 'Star', 'Starter', 'Role', 'Bust']
最终代码应如下所示:
import pandas as pd
r = pd.DataFrame.from_csv('data/historical_projections.csv')
r = r.round(2)
r.columns = ['Pos', 'ID', 'Draft Year', 'Proj SPM', 'Star', 'Starter', 'Role', 'Bust']
print r