常用选项

The following is a walkthrough of the more frequently used display options.

display.max_rows and display.max_columns sets the maximum number of rows and columns displayed when a frame is pretty-printed. Truncated lines are replaced by an ellipsis.

  1. In [23]: df = pd.DataFrame(np.random.randn(7,2))
  2. In [24]: pd.set_option('max_rows', 7)
  3. In [25]: df
  4. Out[25]:
  5. 0 1
  6. 0 0.469112 -0.282863
  7. 1 -1.509059 -1.135632
  8. 2 1.212112 -0.173215
  9. 3 0.119209 -1.044236
  10. 4 -0.861849 -2.104569
  11. 5 -0.494929 1.071804
  12. 6 0.721555 -0.706771
  13. In [26]: pd.set_option('max_rows', 5)
  14. In [27]: df
  15. Out[27]:
  16. 0 1
  17. 0 0.469112 -0.282863
  18. 1 -1.509059 -1.135632
  19. .. ... ...
  20. 5 -0.494929 1.071804
  21. 6 0.721555 -0.706771
  22. [7 rows x 2 columns]
  23. In [28]: pd.reset_option('max_rows')

display.expand_frame_repr allows for the representation of dataframes to stretch across pages, wrapped over the full column vs row-wise.

  1. In [29]: df = pd.DataFrame(np.random.randn(5,10))
  2. In [30]: pd.set_option('expand_frame_repr', True)
  3. In [31]: df
  4. Out[31]:
  5. 0 1 2 3 4 5 6 7 8 9
  6. 0 -1.039575 0.271860 -0.424972 0.567020 0.276232 -1.087401 -0.673690 0.113648 -1.478427 0.524988
  7. 1 0.404705 0.577046 -1.715002 -1.039268 -0.370647 -1.157892 -1.344312 0.844885 1.075770 -0.109050
  8. 2 1.643563 -1.469388 0.357021 -0.674600 -1.776904 -0.968914 -1.294524 0.413738 0.276662 -0.472035
  9. 3 -0.013960 -0.362543 -0.006154 -0.923061 0.895717 0.805244 -1.206412 2.565646 1.431256 1.340309
  10. 4 -1.170299 -0.226169 0.410835 0.813850 0.132003 -0.827317 -0.076467 -1.187678 1.130127 -1.436737
  11. In [32]: pd.set_option('expand_frame_repr', False)
  12. In [33]: df
  13. Out[33]:
  14. 0 1 2 3 4 5 6 7 8 9
  15. 0 -1.039575 0.271860 -0.424972 0.567020 0.276232 -1.087401 -0.673690 0.113648 -1.478427 0.524988
  16. 1 0.404705 0.577046 -1.715002 -1.039268 -0.370647 -1.157892 -1.344312 0.844885 1.075770 -0.109050
  17. 2 1.643563 -1.469388 0.357021 -0.674600 -1.776904 -0.968914 -1.294524 0.413738 0.276662 -0.472035
  18. 3 -0.013960 -0.362543 -0.006154 -0.923061 0.895717 0.805244 -1.206412 2.565646 1.431256 1.340309
  19. 4 -1.170299 -0.226169 0.410835 0.813850 0.132003 -0.827317 -0.076467 -1.187678 1.130127 -1.436737
  20. In [34]: pd.reset_option('expand_frame_repr')

display.large_repr lets you select whether to display dataframes that exceed max_columns or max_rows as a truncated frame, or as a summary.

  1. In [35]: df = pd.DataFrame(np.random.randn(10,10))
  2. In [36]: pd.set_option('max_rows', 5)
  3. In [37]: pd.set_option('large_repr', 'truncate')
  4. In [38]: df
  5. Out[38]:
  6. 0 1 2 3 4 5 6 7 8 9
  7. 0 -1.413681 1.607920 1.024180 0.569605 0.875906 -2.211372 0.974466 -2.006747 -0.410001 -0.078638
  8. 1 0.545952 -1.219217 -1.226825 0.769804 -1.281247 -0.727707 -0.121306 -0.097883 0.695775 0.341734
  9. .. ... ... ... ... ... ... ... ... ... ...
  10. 8 -2.484478 -0.281461 0.030711 0.109121 1.126203 -0.977349 1.474071 -0.064034 -1.282782 0.781836
  11. 9 -1.071357 0.441153 2.353925 0.583787 0.221471 -0.744471 0.758527 1.729689 -0.964980 -0.845696
  12. [10 rows x 10 columns]
  13. In [39]: pd.set_option('large_repr', 'info')
  14. In [40]: df
  15. Out[40]:
  16. <class 'pandas.core.frame.DataFrame'>
  17. RangeIndex: 10 entries, 0 to 9
  18. Data columns (total 10 columns):
  19. 0 10 non-null float64
  20. 1 10 non-null float64
  21. 2 10 non-null float64
  22. 3 10 non-null float64
  23. 4 10 non-null float64
  24. 5 10 non-null float64
  25. 6 10 non-null float64
  26. 7 10 non-null float64
  27. 8 10 non-null float64
  28. 9 10 non-null float64
  29. dtypes: float64(10)
  30. memory usage: 880.0 bytes
  31. In [41]: pd.reset_option('large_repr')
  32. In [42]: pd.reset_option('max_rows')

display.max_colwidth sets the maximum width of columns. Cells of this length or longer will be truncated with an ellipsis.

  1. In [43]: df = pd.DataFrame(np.array([['foo', 'bar', 'bim', 'uncomfortably long string'],
  2. ....: ['horse', 'cow', 'banana', 'apple']]))
  3. ....:
  4. In [44]: pd.set_option('max_colwidth',40)
  5. In [45]: df
  6. Out[45]:
  7. 0 1 2 3
  8. 0 foo bar bim uncomfortably long string
  9. 1 horse cow banana apple
  10. In [46]: pd.set_option('max_colwidth', 6)
  11. In [47]: df
  12. Out[47]:
  13. 0 1 2 3
  14. 0 foo bar bim un...
  15. 1 horse cow ba... apple
  16. In [48]: pd.reset_option('max_colwidth')

display.max_info_columns sets a threshold for when by-column info will be given.

  1. In [49]: df = pd.DataFrame(np.random.randn(10,10))
  2. In [50]: pd.set_option('max_info_columns', 11)
  3. In [51]: df.info()
  4. <class 'pandas.core.frame.DataFrame'>
  5. RangeIndex: 10 entries, 0 to 9
  6. Data columns (total 10 columns):
  7. 0 10 non-null float64
  8. 1 10 non-null float64
  9. 2 10 non-null float64
  10. 3 10 non-null float64
  11. 4 10 non-null float64
  12. 5 10 non-null float64
  13. 6 10 non-null float64
  14. 7 10 non-null float64
  15. 8 10 non-null float64
  16. 9 10 non-null float64
  17. dtypes: float64(10)
  18. memory usage: 880.0 bytes
  19. In [52]: pd.set_option('max_info_columns', 5)
  20. In [53]: df.info()
  21. <class 'pandas.core.frame.DataFrame'>
  22. RangeIndex: 10 entries, 0 to 9
  23. Columns: 10 entries, 0 to 9
  24. dtypes: float64(10)
  25. memory usage: 880.0 bytes
  26. In [54]: pd.reset_option('max_info_columns')

display.max_info_rows: df.info() will usually show null-counts for each column. For large frames this can be quite slow.max_info_rows and max_info_cols limit this null check only to frames with smaller dimensions then specified. Note that you can specify the option df.info(null_counts=True) to override on showing a particular frame.

  1. In [55]: df = pd.DataFrame(np.random.choice([0,1,np.nan], size=(10,10)))
  2. In [56]: df
  3. Out[56]:
  4. 0 1 2 3 4 5 6 7 8 9
  5. 0 0.0 1.0 1.0 0.0 1.0 1.0 0.0 NaN 1.0 NaN
  6. 1 1.0 NaN 0.0 0.0 1.0 1.0 NaN 1.0 0.0 1.0
  7. 2 NaN NaN NaN 1.0 1.0 0.0 NaN 0.0 1.0 NaN
  8. 3 0.0 1.0 1.0 NaN 0.0 NaN 1.0 NaN NaN 0.0
  9. 4 0.0 1.0 0.0 0.0 1.0 0.0 0.0 NaN 0.0 0.0
  10. 5 0.0 NaN 1.0 NaN NaN NaN NaN 0.0 1.0 NaN
  11. 6 0.0 1.0 0.0 0.0 NaN 1.0 NaN NaN 0.0 NaN
  12. 7 0.0 NaN 1.0 1.0 NaN 1.0 1.0 1.0 1.0 NaN
  13. 8 0.0 0.0 NaN 0.0 NaN 1.0 0.0 0.0 NaN NaN
  14. 9 NaN NaN 0.0 NaN NaN NaN 0.0 1.0 1.0 NaN
  15. In [57]: pd.set_option('max_info_rows', 11)
  16. In [58]: df.info()
  17. <class 'pandas.core.frame.DataFrame'>
  18. RangeIndex: 10 entries, 0 to 9
  19. Data columns (total 10 columns):
  20. 0 8 non-null float64
  21. 1 5 non-null float64
  22. 2 8 non-null float64
  23. 3 7 non-null float64
  24. 4 5 non-null float64
  25. 5 7 non-null float64
  26. 6 6 non-null float64
  27. 7 6 non-null float64
  28. 8 8 non-null float64
  29. 9 3 non-null float64
  30. dtypes: float64(10)
  31. memory usage: 880.0 bytes
  32. In [59]: pd.set_option('max_info_rows', 5)
  33. In [60]: df.info()
  34. <class 'pandas.core.frame.DataFrame'>
  35. RangeIndex: 10 entries, 0 to 9
  36. Data columns (total 10 columns):
  37. 0 float64
  38. 1 float64
  39. 2 float64
  40. 3 float64
  41. 4 float64
  42. 5 float64
  43. 6 float64
  44. 7 float64
  45. 8 float64
  46. 9 float64
  47. dtypes: float64(10)
  48. memory usage: 880.0 bytes
  49. In [61]: pd.reset_option('max_info_rows')

display.precision sets the output display precision in terms of decimal places. This is only a suggestion.

  1. In [62]: df = pd.DataFrame(np.random.randn(5,5))
  2. In [63]: pd.set_option('precision',7)
  3. In [64]: df
  4. Out[64]:
  5. 0 1 2 3 4
  6. 0 -2.0490276 2.8466122 -1.2080493 -0.4503923 2.4239054
  7. 1 0.1211080 0.2669165 0.8438259 -0.2225400 2.0219807
  8. 2 -0.7167894 -2.2244851 -1.0611370 -0.2328247 0.4307933
  9. 3 -0.6654779 1.8298075 -1.4065093 1.0782481 0.3227741
  10. 4 0.2003243 0.8900241 0.1948132 0.3516326 0.4488815
  11. In [65]: pd.set_option('precision',4)
  12. In [66]: df
  13. Out[66]:
  14. 0 1 2 3 4
  15. 0 -2.0490 2.8466 -1.2080 -0.4504 2.4239
  16. 1 0.1211 0.2669 0.8438 -0.2225 2.0220
  17. 2 -0.7168 -2.2245 -1.0611 -0.2328 0.4308
  18. 3 -0.6655 1.8298 -1.4065 1.0782 0.3228
  19. 4 0.2003 0.8900 0.1948 0.3516 0.4489

display.chop_threshold sets at what level pandas rounds to zero when it displays a Series of DataFrame. This setting does not change the precision at which the number is stored.

  1. In [67]: df = pd.DataFrame(np.random.randn(6,6))
  2. In [68]: pd.set_option('chop_threshold', 0)
  3. In [69]: df
  4. Out[69]:
  5. 0 1 2 3 4 5
  6. 0 -0.1979 0.9657 -1.5229 -0.1166 0.2956 -1.0477
  7. 1 1.6406 1.9058 2.7721 0.0888 -1.1442 -0.6334
  8. 2 0.9254 -0.0064 -0.8204 -0.6009 -1.0393 0.8248
  9. 3 -0.8241 -0.3377 -0.9278 -0.8401 0.2485 -0.1093
  10. 4 0.4320 -0.4607 0.3365 -3.2076 -1.5359 0.4098
  11. 5 -0.6731 -0.7411 -0.1109 -2.6729 0.8645 0.0609
  12. In [70]: pd.set_option('chop_threshold', .5)
  13. In [71]: df
  14. Out[71]:
  15. 0 1 2 3 4 5
  16. 0 0.0000 0.9657 -1.5229 0.0000 0.0000 -1.0477
  17. 1 1.6406 1.9058 2.7721 0.0000 -1.1442 -0.6334
  18. 2 0.9254 0.0000 -0.8204 -0.6009 -1.0393 0.8248
  19. 3 -0.8241 0.0000 -0.9278 -0.8401 0.0000 0.0000
  20. 4 0.0000 0.0000 0.0000 -3.2076 -1.5359 0.0000
  21. 5 -0.6731 -0.7411 0.0000 -2.6729 0.8645 0.0000
  22. In [72]: pd.reset_option('chop_threshold')

display.colheader_justify controls the justification of the headers. The options are ‘right’, and ‘left’.

  1. In [73]: df = pd.DataFrame(np.array([np.random.randn(6), np.random.randint(1,9,6)*.1, np.zeros(6)]).T,
  2. ....: columns=['A', 'B', 'C'], dtype='float')
  3. ....:
  4. In [74]: pd.set_option('colheader_justify', 'right')
  5. In [75]: df
  6. Out[75]:
  7. A B C
  8. 0 0.9331 0.3 0.0
  9. 1 0.2888 0.2 0.0
  10. 2 1.3250 0.2 0.0
  11. 3 0.5892 0.7 0.0
  12. 4 0.5314 0.1 0.0
  13. 5 -1.1987 0.7 0.0
  14. In [76]: pd.set_option('colheader_justify', 'left')
  15. In [77]: df
  16. Out[77]:
  17. A B C
  18. 0 0.9331 0.3 0.0
  19. 1 0.2888 0.2 0.0
  20. 2 1.3250 0.2 0.0
  21. 3 0.5892 0.7 0.0
  22. 4 0.5314 0.1 0.0
  23. 5 -1.1987 0.7 0.0
  24. In [78]: pd.reset_option('colheader_justify')