• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / NumPy / NumPy 2d-array / 3 Basic Commands to Manipulate NumPy 2d-arrays

3 Basic Commands to Manipulate NumPy 2d-arrays

June 14, 2019 by cmdlinetips

NumPy or Numerical Python is one of the packages in Python for all things computing with numerical values. Learning NumPy makes one’s life much easier to compute with multi-dimensional arrays and matrices. A huge collection of very useful mathematical functions available to operate on these arrays these arrays makes it one of the powerful environment for scientific computing in Python. In an earlier post,

  • 12 Basic Commands with NumPy Array

we saw how we can get started using NumPy’s 1d-arrays with some basic operations on it. Here, we are will going over the 3 most basic and useful commands to learn NumPy 2d-array.

Load NumPy Package

Let us load the numpy package with the shorthand np.

>import mumpy as np

How to create 2d-array with NumPy?

Let us create 2d-array with NumPy, such that it has 2-rows and three columns. We can simply use two tuples of size 3 with np.array function as

# create a 2d-array of shape 2 x 3
>b = np.array([(1.5,7,8), (41,45,46)])
# print the 2d-array 
>print(b)
[[  1.5   7.    8. ]
 [ 41.   45.   46. ]]

How to transpose NumPy array?

We can use transpose() function to transpose a 2d-array in NumPy.

# transpose the array
# note the shape is 3 x 2 np.transpose(b) 
>i = np.transpose(b)
array([[  1.5,  41. ],
       [  7. ,  45. ],
       [  8. ,  46. ]])

How to flatten a nd-array to 1d-array?

We can use ravel() function in NumPy to flatten 2d-array into 1d-array.

>b.ravel()
array([  1.5,   7. ,   8. ,  41. ,  45. ,  46. ])

How to re-shape NumPy array?

We can use reshape() function to change the shape of 2d-array.

>b.reshape(3,2)
array([[  1.5,   7. ],
       [  8. ,  41. ],
       [ 45. ,  46. ]])

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

NumPy is the fundamental package for scientific computing with Python. It contains among other things: a powerful N-dimensional array object sophisticated (broadcasting) functions tools for integrating C/C++ and Fortran code useful linear algebra, Fourier transform, and random number capabilities10 Basic Arithmetic Operations with NumPy array Default Thumbnail12 Basic Commands with NumPy Array Default ThumbnailHow To Concatenate Arrays in NumPy? Default Thumbnail6 Most Useful dplyr Commands to Manipulate a Data Frame in R

Filed Under: NumPy 2d-array, NumPy ravel, NumPy reshape, NumPy transpose Tagged With: NumPy 2d-array, NumPy ravel, NumPy reshape, NumPy transpose

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version