Programmatically convert pandas dataframe to markdown table

Given a pandas dataframe, we have to programmatically convert pandas dataframe to markdown table.
Submitted by Pranit Sharma, on September 13, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Converting pandas dataframe to markdown table

To programmatically convert pandas DataFrame to markdown table, we will directly use to_markdown() method.

Let us understand with the help of an example,

Python code to programmatically convert pandas dataframe to markdown table

# Importing pandas package
import pandas as pd

# Creating a dictionary
d = {
    'id':[1,2,3,4,5],
    'Name':['Raghu','Rajiv','Ranvijay','Prince','Nikhil'],
    'age':[40,40,38,33,40],
    'location':['Mumbai','Mumbai','Cgandigarh','Amritsar','Delhi'],
    'salary':[12000,12000,15000,14000,15000],
    'in-hand':[10000,10000,13500,12500,13500],
    'sex':['male','male','male','male','male']
}

# Creating a DataFrame
df = pd.DataFrame(d)

# Display original DataFrame
print("Original DataFrame:\n",df,"\n")

# Displaying the data in markdown table format
print(df.to_markdown())

Output:

Example: Programmatically convert pandas dataframe to markdown table

Python Pandas Programs »



ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.