Python is used in various fields such as data analysis, web development, and automation. Pandas is used for data analysis and manipulation. Pandas offer a powerful tool called DataFrame, which allows you to manipulate, analyze, and visualize data in a tabular format.
In this article, we will discuss how to print the entire DataFrame in Python using different methods. We will also look at customizing the DataFrame output and saving it as a file in various formats.
[lwptoc]
Installing Pandas
To follow along with this article, you need to have Pandas installed on your system. If you haven’t already installed it, you can do so using the following command:
pip install pandas
Displaying the DataFrame
Let’s check how to display the DataFrame data:
Importing Data
Before we can display a DataFrame, we need to import some data. For this example, we will create a simple DataFrame from a dictionary.
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [25, 30, 35, 40],
'Country': ['USA', 'UK', 'Australia', 'Canada']
}
df = pd.DataFrame(data)
1) Using the print()
Function
The simplest way to display the entire DataFrame is by using the print()
function:
print(df)
2) Using the display()
Function
Another way to display the DataFrame is by using the display()
function from the IPython.display module. This method provides a more visually appealing output when using Jupyter Notebook or JupyterLab.
from IPython.display import display
display(df)
Customizing DataFrame Output
In this section we will discuss how to format the max rows columns and also how to format the floats:
Changing Maximum Rows and Columns
By default, Pandas limits the number of rows and columns displayed. To display the entire DataFrame, we can adjust these settings using the pd.set_option()
function:
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
Formatting Floats in DataFrames
To customize the display of float values in a DataFrame, use the pd.options.display.float_format
option:
pd.options.display.float_format = '{:,.2f}'.format
Saving the DataFrame as a File
Lets discuss how to save the DataFrame in various formats:
1) Saving as CSV
To save the DataFrame as a CSV file, use the to_csv()
method:
df.to_csv('data.csv', index=False)
2) Saving as Excel
To save the DataFrame as an Excel file, first, install the openpyxl
package:
pip install openpyxl
Then, use the <span class="hljs-built_in">to_excel</span>()
method:
df.to_excel('data.xlsx', index=False, engine='openpyxl')
3) Saving as HTML
To save the DataFrame as an HTML file, use the to_html()
method:
df.to_html('data.html', index=False)
Conclusion
In this article, we discussed various ways to print the entire DataFrame in Python using the print()
and display()
functions.
We also looked at customizing the DataFrame output, including changing the maximum rows and columns and formatting float values. We also explored different methods for saving the DataFrame as a file in various formats, such as CSV, Excel, and HTML.
Leave a Reply