Pandas can perform a range of I/O Operations such as reading and writing data from various file formats (CSV, Excel, JSON, etc.). The commands for reading CSV and JSON data are read_csv()
and read_json()
.
For viewing data, you can use head()
and tail()
methods. The head()
method returns the headers and a specified number of rows, starting from the top, and the tail()
method returns the headers and a specified number of rows, starting from the bottom.
These operations for reading and viewing the data are illustrated in the code in Exhibit 25.54.
import pandas as pd
# Load a CSV file into a DataFrame:
df1 = pd.read_csv('data/exercise_metrics.csv')
# Load the JSON file into a DataFrame:
df2 = pd.read_json('data/exercise_metrics.json')
'''
Viewing:
The head() method returns the headers and a specified number of rows, starting from the top.
The tail() method returns the headers and a specified number of rows, starting from the bottom.
Default is 5 rows.
'''
# Get a quick overview by printing the first 10 rows of data.cvs
print(df1.head(10))
print("\nRow 3:") # Go to the next line and print "Row 3:"
print(df1.iloc[3]) # Print the 3rd row
# Print the last 5 rows of data.json
print("\n") # leave 1 blank line
print(df2.tail())
Duration Pulse Maxpulse Calories 0 60 110 130 409.1 1 60 117 145 479.0 2 60 103 135 340.0 3 45 109 175 282.4 4 45 117 148 406.0 5 60 102 127 300.0 6 60 110 136 374.0 7 45 104 134 253.3 8 30 109 133 195.1 9 60 98 124 269.0 Row 3: Duration 45.0 Pulse 109.0 Maxpulse 175.0 Calories 282.4 Name: 3, dtype: float64 Duration Pulse Maxpulse Calories 164 60 105 140 290.8 165 60 110 145 300.4 166 60 115 145 310.2 167 75 120 150 320.4 168 75 125 150 330.4
Use the Search Bar to find content on MarketingMind.
Contact | Privacy Statement | Disclaimer: Opinions and views expressed on www.ashokcharan.com are the author’s personal views, and do not represent the official views of the National University of Singapore (NUS) or the NUS Business School | © Copyright 2013-2025 www.ashokcharan.com. All Rights Reserved.