Отображение фрейма данных Pandas в виде таблицы
В этой статье мы увидим, как мы можем отобразить DataFrame в виде таблицы с границами вокруг строк и столбцов. DataFrame необходимо отображать в виде таблицы, так как это помогает в правильной и простой визуализации данных. Теперь давайте рассмотрим несколько способов с помощью примеров, с помощью которых мы можем этого добиться.
Example 1 : One way to display a dataframe in the form of a table is by using the display() function of IPython.display.
# importing the modulesfrom IPython.display import displayimport pandas as pd # creating a DataFramedict = {"Name" : ["Martha", "Tim", "Rob", "Georgia"], "Maths" : [87, 91, 97, 95], "Science" : [83, 99, 84, 76]}df = pd.DataFrame(dict) # displaying the DataFramedisplay(df) |
Output :
Example 2: In this example we’ll use DataFrame.style. It returns a Styler object, which has useful methods for formatting and displaying DataFrames.
# importing the moduleimport pandas as pd # creating a DataFramedict = {"Name" : ["Martha", "Tim", "Rob", "Georgia"], "Maths" : [87, 91, 97, 95], "Science" : [83, 99, 84, 76]}df = pd.DataFrame(dict) # displaying the DataFramedf.style |
Output :
Example 3 : Using DataFrame.style we can also add different styles to our dataframe table. Like, in this example we’ll display all the values greater than 90 using the blue colour and rest with black. To achieve this we’ll use DataFrame.style.applymap() to traverse through all the values of the table and apply the style.
# importing the modulesimport pandas as pdimport numpy as np def color_negative_red(val): """ Takes a scalar and returns a string with the css property `"color: red"` for negative strings, black otherwise. """ color = "blue" if val > 90 else "black" return "color: % s" % color # creating a DataFramedict = {"Maths" : [87, 91, 97, 95], "Science" : [83, 99, 84, 76]}df = pd.DataFrame(dict) # displaying the DataFramedf.style.applymap(color_negative_red) |
Output :
Example 4 :We can also use a library called tabulate for this purpose. It is a library which contains different styles in which dataframes can be displayed. In this example we’ll use the "psql" style.
# importing the modulesfrom tabulate import tabulateimport pandas as pd # creating a DataFramedict = {"Name":["Martha", "Tim", "Rob", "Georgia"], "Maths":[87, 91, 97, 95], "Science":[83, 99, 84, 76]}df = pd.DataFrame(dict) # displaying the DataFrameprint(tabulate(df, headers = "keys", tablefmt = "psql")) |
Output :
Below are all the styles that you can use :
- “plain”
- “simple”
- “github”
- “grid”
- “fancy_grid”
- “pipe”
- “orgtbl”
- “jira”
- “presto”
- “pretty”
- “psql”
- “rst”
- “mediawiki”
- “moinmoin”
- “youtrack”
- “html”
- “latex”
- “latex_raw”
- “latex_booktabs”
- “textile”
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course