Skip to Main Content

Python for Basic Data Analysis

Start your data science journey with Python. Learn practical Python programming skills for basic data manipulation and analysis.

Creating arrays with lists

Create arrays using lists

Let's begin with the basics of NumPy. There are different ways to create NumPy arrays but the easiest way will be to use something that we are all familiar with, which are lists, and pass it to the array() function.

Syntax

nameofarray = np.array(lists)


Below are some examples. Click on the Run button in each trinket to view the results.


Create a rank 1 (1D) array from a list
 


Create a rank 2 (2D) array from a list
 

Check data type in arrays

To find out what data types are in an array, you can use .dtype

Syntax

print(myArray.dtype)

Editing elements in array

You can edit an element in an array by referring to its index.

Recall that  myArray = [1 2 3 4 5 6 7].  You can change the first element of myArray from 1 to 20 by referring to its index (which is 0).
 

myArray[0] = 20

 

Shape of array

You can also check the shape of an array (i.e. number of rows and columns) using .shape

Video Guide

Exercises

1. Create a rank 2 (2D) array that resembles the matrix below.

[[11 12 13 14]
 [15 16 17 18]]


2. Find out the shape of the above array