NumPy has an extensive list of methods to generate random arrays and single numbers, or to randomly shuffle arrays. Continue reading “74. Using NumPy to generate random numbers, or shuffle arrays”

# Category: NumPy and Pandas

# 64. NumPy: Setting width and number of decimal places in NumPy print output

The function below may be used to set both the number of decimal places and the fixed width of NumPy print out. If no width is given it defaults to zero (no extra padding added). Setting width will ensure alignment of output. Continue reading “64. NumPy: Setting width and number of decimal places in NumPy print output”

# 41. Matplotlib: Histograms (and obtaining histogram data with NumPy)

Matplotlib has an easy method for plotting data. NumPy has an easy method for obtaining histogram data.

## Plotting histograms with Matplotlib

Plotting a histogram with a defined number of bins: Continue reading “41. Matplotlib: Histograms (and obtaining histogram data with NumPy)”

# 40. Removing duplicate data in NumPy and Pandas

Both NumPy and Pandas offer easy ways of removing duplicate rows. Pandas offers a more powerful approach if you wish to remove rows that are partly duplicated.

## NumPy

With numpy we use *np.unique()* to remove duplicate rows or columns (use the argument *axis=0* for unique rows or *axis=1* for unique columns). Continue reading “40. Removing duplicate data in NumPy and Pandas”

# 35. Array maths in NumPy

NumPy allows easy standard mathematics to be performed on arrays, a well as moire complex linear algebra such as array multiplication.

Lets begin by building a couple of arrays. We’ll use the np.arange method to create an array of numbers in range 1 to 12, and then reshape the array into a 3 x 4 array. Continue reading “35. Array maths in NumPy”

# 34. Iterating through columns and rows in NumPy and Pandas

Using *apply_along_axis* (NumPy) or *apply* (Pandas) is a more Pythonic way of iterating through data in NumPy and Pandas (see related tutorial here). But there may be occasions you wish to simply work your way through rows or columns in NumPy and Pandas. Here is how it is done. Continue reading “34. Iterating through columns and rows in NumPy and Pandas”

# 33. Subgrouping data in Pandas with groupby

A very powerful feature in Pandas is to use groupby to create groups of data. Each group may then be further acted on as if it were an independent dataframe. This allows for very sophisticated operations broken down by group.

Here we will create a very simple example to illustrate this. Continue reading “33. Subgrouping data in Pandas with groupby”