How to make a dataset in Python

Create a dataset in Python using an example: Learn how to use Pandas and Numpy to build a dataset from scratch.

Creating a Dataset in Python with an Example

A dataset is a collection of data that can be used for various purposes such as analysis, machine learning, and more. In Python, there are a few ways to create a dataset. We can use libraries like Pandas, Numpy, and others to create a dataset. We can also use lists and dictionaries to create a dataset. In this example, we will use a list of dictionaries to create a dataset.

Let's say we have some data about people. We want to store this data in a dataset. We can create a list of dictionaries to store this data:


people_data = [
    {'name': 'John', 'age': 22, 'city': 'New York'},
    {'name': 'Mary', 'age': 24, 'city': 'Boston'},
    {'name': 'Paul', 'age': 26, 'city': 'Chicago'},
    {'name': 'Jane', 'age': 28, 'city': 'Los Angeles'}
]

We have now created a dataset with information about four people. We can access the data by looping through the list and accessing the keys of the dictionaries:


for person in people_data:
    # access the name
    name = person['name']

    # access the age
    age = person['age']

    # access the city
    city = person['city']

    print(f'{name} is {age} years old and lives in {city}')

When we run this code, we get the following output:


John is 22 years old and lives in New York
Mary is 24 years old and lives in Boston
Paul is 26 years old and lives in Chicago
Jane is 28 years old and lives in Los Angeles

We have now successfully created a dataset in Python and accessed the data. This is a simple example of how to create a dataset in Python. We can use this same technique to create more complex datasets with multiple fields and data points.

Answers (0)