程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Incomplete display of pandas data? Come and learn about these setup techniques!

編輯:Python
  • author : Han Xinzi @ShowMeAI
  • Data analysis practical series :http://www.showmeai.tech/tutorials/40
  • This paper addresses :http://www.showmeai.tech/article-detail/285
  • Statement : copyright , For reprint, please contact the platform and the author and indicate the source
  • Collection ShowMeAI See more

Pandas Data display problems

We are applying Python Conduct Data analysis mining and machine learning when , The most commonly used tool library is Pandas, It can help us process and analyze data quickly .

Yes Pandas Unfamiliar students , Be sure to learn this treasure tool library !ShowMeAI I gave you a detailed tutorial , Can be in Python Data analysis tutorial View in , We also made Pandas Quick reference table , It is convenient for everyone to quickly find the required functions . If you like learning with videos , The recommended B Stand here Quick combat tutorial .

But in the use of Pandas when , We often encounter problems like the following , It affects us to check the data for details .

Long text cannot be displayed in full

For very long fields, the display may be incomplete , As shown in the figure below ,URL Shortened display .

Scientific counting shows the loss of detail

Pandas By default 『 Scientific enumeration 』 Show large floating point numbers , for example 1000000.5 Is shown as 1.000e+06 . For large numbers , There may be the following display , As a result, we cannot see the specific value .

The decimal precision is inconsistent

For floating-point field columns ,Pandas There may be different bit precision . For example, in the figure below ,col_1 To one decimal place , and col_2 Accurate to three decimal places . Sometimes there may be information differences due to inconsistent accuracy .

In this article ,ShowMeAI How to use Pandas Customize settings to solve problems such as the above . The main settings include the following :

  • Customize the number of rows to display
  • Customize the number of columns to display
  • Custom column width
  • Keep the decimal precision between floating-point columns consistent
  • Disable scientific notation
  • Other uses

Be careful : The above settings only change the display and presentation of data , Actually, it will not affect Dataframe Stored data .

Pandas Customize display settings

Customize the number of lines displayed

Print big Dataframe( Data with a large number of rows and columns ) when ,Pandas Before default display 5 After the row and 5 That's ok , As shown in the figure below .

We can set the display options display.max_rows To change the number of rows to display , For example, we set it to 4.

pd.set_option("diaply.max_row", 4)
df

We can use the reset option pd.reset_option("display.max_rows") Restore the default row count display setting .

Customize the number of columns displayed

Same thing , We can set display.max_columns Custom output Dataframe The number of columns to display .

pd.set_option("diaply.max_columns", 6)
df

We can even set pd.set_option('display.max_columns',`` ``None) To display all columns ( But you need to pay attention to memory usage , This operation may make Jupyter Notebook It takes up a lot of resources ).

We can also use it pd.reset_option("display.max_columns") Reset returns to the default settings .

Custom column width

In the following illustration , We can't see the full text of the first two lines , Because their characters are too long ( It's longer than 50).

We set up display.max_colwidth To adjust to 70, You can see the full text , As shown in the figure below .

pd.set_option("diaply.max_colwidth", 70)
df

The operation of resetting this setting is still pd.reset_option("display.max_colwidth").

Set the decimal places of the field to be consistent

In the previous example ,col_1 and col_2 The decimal precision of is inconsistent :

We can set display.float_format to "{:.2f}".format Make the format consistent , As shown in the figure below .

This option only affects floating-point columns , Without affecting the integer column .

pd.set_option("diaply.float_format", "{:.2f}".format)
df

The operation of resetting this setting is pd.reset_option("display.float_format")

Prohibition of scientific counting

Pandas By default, large floating-point values are displayed in scientific counting .

By setting display.float_format to "{:,.2f}".format, We can add delimiters for thousands .

pd.set_option("diaply.float_format", "{:,.2f}".format)
df

We can even add currency symbols in front of numerical values , For example, we put display.float_format Set to "$ {:,.2f}".format, The results are as follows :

pd.set_option("diaply.float_format", "$ {:,.2f}".format)
df

Other settings

Listed above are some of the most commonly used settings , If we can't remember the names of these settings , Or we want to know all the display settings that can be adjusted , What can I do ? In fact, you can use pd.describe_option() Get a list of all available display settings .

For a specific display setting , Can be in pd.describe_option() Pass in the name of the display setting you want to adjust to get the usage details , For example, we run pd.describe_option("max_rows") The description will be printed display.max_rows Use details , As shown in the figure below .

pd.describe_option("max_rows")

Reference material

  • Pandas set an option : https://pandas.pydata.org/docs/user_guide/options.html
  • Python Data analysis tutorial : http://www.showmeai.tech/tutorials/33
  • Pandas Quick reference table :http://www.showmeai.tech/article-detail/101
  • Pandas Quick combat tutorial : https://www.bilibili.com/video/BV1M64y187bz


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved