w3resource

Pandas styling Exercises: Write a Pandas program to display the dataframe in table style and border around the table and not around the rows


14. Display DataFrame in Table Style with Outer Border

Create a dataframe of ten rows, four columns with random values. Write a Pandas program to display the dataframe in table style and border around the table and not around the rows.

Sample Solution :

Python Code :

import pandas as pd
import numpy as np
np.random.seed(24)
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],
               axis=1)
df.iloc[0, 2] = np.nan
df.iloc[3, 3] = np.nan
df.iloc[4, 1] = np.nan
df.iloc[9, 4] = np.nan
print("Original array:")
print(df)
print("\nDataframe - table style and border around the table and not around the rows:")
df.style.set_table_styles([{'selector':'','props':[('border','4px solid #7a7')]}])

Original array:

Original array:
      A         B         C         D         E
0   1.0  1.329212       NaN -0.316280 -0.990810
1   2.0 -1.070816 -1.438713  0.564417  0.295722
2   3.0 -1.626404  0.219565  0.678805  1.889273
3   4.0  0.961538  0.104011       NaN  0.850229
4   5.0       NaN  1.057737  0.165562  0.515018
5   6.0 -1.336936  0.562861  1.392855 -0.063328
6   7.0  0.121668  1.207603 -0.002040  1.627796
7   8.0  0.354493  1.037528 -0.385684  0.519818
8   9.0  1.686583 -1.325963  1.428984 -2.089354
9  10.0 -0.129820  0.631523 -0.586538       NaN

Dataframe - table style and border around the table and not around the rows:

Sample Output:

Python Pandas: pandas styling exercise-14 output

Download the Jupyter Notebook from here.


For more Practice: Solve these Related Problems:

  • Write a Pandas program to render a dataframe as an table with a border around the entire table but not around individual rows.
  • Write a Pandas program to apply CSS styling that places a solid border only on the outer edge of a dataframe table.
  • Write a Pandas program to style a dataframe so that a border is drawn around the table while internal cell borders remain hidden.
  • Write a Pandas program to modify the Styler properties to display a dataframe with an overall table border and no cell borders.

Go to:


Previous: Create a dataframe of ten rows, four columns with random values. Write a Pandas program to make a gradient color on all the values of the said dataframe.

Next: Create a dataframe of ten rows, four columns with random values. Write a Pandas program to display bar charts in dataframe on specified columns.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.