w3resource

Loading a Dataset from CSV Using Pandas


Pandas: Machine Learning Integration Exercise-1 with Solution


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().

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.



Follow us on Facebook and Twitter for latest update.