2 dimensional arrays in python

By | January 30, 2020
Spread the love

2 dimensional arrays in python. Learn to create 2 dimensional arrays in python with example

2 dimensional arrays in python

2D Numpy Arrays

numpy.ndarray

numpy means that it is a type that is defined in the numpy package.

nd stands for n dimensional array

You can create 1,2,3 and even upto 7 dimensional array but we are discussing here only two dimensional arrays

You can create a 2d array from a regular python list

Example of 2d numpy array

2dArray= np.array = ([[1.37,1.99,1.74,1.33],
[50.5,55.5,23.23,74.4]])

The array which we have created above have 2 rows and 4 columns

2dArray.shape

Shape is an attribute of 2dArray which tells us more information about what the datastructure look like.
means this wil tell you about that how many rows and columns are there in your two dimensional array

functions have brackets () but shap does not have it means that attributes does not have () brackets

(2,4) # 2 rows, 4 columns

Note:

An array can contain only single type(data type)
If you changed one element of flaot array or int array to strings then all the elements in that array will be changed(coversed)
to strings to end up with a homogenous array

2D array is an improved form of list of list. You can perform any type of calculations. You can do more advance subsetting with 2D array.

Subsetting an 2D array:

Suppose you want to access the first row and 1st element of the array

Then you can write like

2dArray[0][1]

[0] is used for row means first brackets is used for row.
[1] is used for columns means the second bracket is used for column.

If you want to access the 3 element in 2nd row then you will write like

2dArray[1][2]

Now you can access the 2nd row and 3rd element from your array

Remember indexing of arrays strting from zero.

You can also write 2dArray[1,2] instead of 2dArray[1][2]

The result will be the same in both cases both cases the first digit represnt the rows and 2nd degit represent the columns.

2dArray[:,1:3]

if there is a 2D array which have two rows one contain weight and other contain height If you want to print the height and weight of 2nd and third element
then you can do like this. This will print the 2 columns and 3rd index is no inculded.

2dArray[1,:]

: intersection will print the entire second row

will print the entire second row

  •  
    78
    Shares
  • 76
  •  
  •  
  •  
  • 2
  •  
  •  
  •   
  •   
  •   
  •   
  •  

Leave a Reply

Your email address will not be published. Required fields are marked *