Loading a Dataset from CSV Using Pandas
1. Loading Dataset from CSV
Write a Pandas program that loads a Dataset from a CSV file.
This exercise demonstrates how to load a dataset using Pandas from a CSV file.
Sample Solution :
Code :
import pandas as pd
# Load a dataset from a CSV file
df = pd.read_csv('data.csv')
# Display the first few rows of the dataset
print(df.head())
Output:
ID Name Age Gender Salary Target 0 1 Sara 25.0 Female 50000.0 0 1 2 Ophrah 30.0 Male 60000.0 1 2 3 Torben 22.0 Male 70000.0 0 3 4 Masaharu 35.0 Male 80000.0 1 4 5 Kaya NaN Female 55000.0 0
Explanation:
- Imported the Pandas library.
- Loaded a dataset from a CSV file using pd.read_csv().
- Displayed the first few rows of the dataset using df.head().
For more Practice: Solve these Related Problems:
- Write a Pandas program to load a CSV file with a non-standard delimiter and verify the DataFrame dimensions.
- Write a Pandas program to load a CSV file from a URL and display the first five rows sorted by a specific column.
- Write a Pandas program to load a large CSV file in chunks and aggregate a numeric column from each chunk.
- Write a Pandas program to load a CSV file with missing header information and assign custom column names.
Python-Pandas Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.