In this problem we have to sort a Pandas series. We will define an unsorted pandas series and will sort it using the sort_values() function in the Pandas library.AlgorithmStep 1: Define Pandas series. Step 2: Sort the series using sort_values() function. Step 3: Print the sorted series.Example Codeimport pandas as pd panda_series = pd.Series([18, 15, 66, 92, 55, 989]) print("Unsorted Pandas Series: ", panda_series) panda_series_sorted = panda_series.sort_values(ascending = True) print("Sorted Pandas Series: ", panda_series_sorted)OutputUnsorted Pandas Series: 0 18 1 15 2 66 3 92 4 55 5 ... Read More
First, we can create an array matrix with some np.nan value, and using imshow method, we can create a diagram for that matrix.StepsCreate a new figure, or activate an existing figure.Add an `~.axes.Axes` to the figure as part of a subplot arrangement, nrows = 1, ncols = 1, index = 1.Create a 2D array with np.nan.Display data as an image, i.e., on a 2D regular raster.Use the draw() method which draws the drawing at the given location.To show the figure, use the plt.show() method.Exampleimport numpy as np import matplotlib.pyplot as plt f = plt.figure() ax = f.add_subplot(111) a = ... Read More
This is a simple program wherein we have to reverse a numpy array. We will use numpy.flip() function for the same.AlgorithmStep 1: Import numpy. Step 2: Define a numpy array using numpy.array(). Step 3: Reverse the array using numpy.flip() function. Step 4: Print the array.Example Codeimport numpy as np arr = np.array([10,20,30,40,50]) print("Original Array: ", arr) arr_reversed = np.flip(arr) print("Reversed Array: ", arr_reversed)OutputOriginal Array: [10 20 30 40 50] Reversed Array: [50 40 30 20 10]
In this program, we have to print elements of a numpy array in a given range. The different numpy functions used are numpy.where() and numpy.logical_and().AlgorithmStep 1: Define a numpy array. Step 2: Use np.where() and np.logical_and() to find the numbers within the given range. Step 3: Print the result.Example Codeimport numpy as np arr = np.array([1,3,5,7,10,2,4,6,8,10,36]) print("Original Array:",arr) result = np.where(np.logical_and(arr>=4, arr
To get the axes instance, we will use the subplots() method.StepsMake a list of years.Make a list of populations in that year.Get the number of labels using np.arrange(len(years)) method.Set the width of the bars.Create fig and ax variables using subplots() method, where default nrows and ncols are 1.Set the Y-axis label of the figure using set_ylabel().Set the title of the figure, using set_title() method.Set the x-ticks with x that is created in step 3, using set_xticks method.Set the xtick_labels with years data, using set_xticklabels method.Use plt.show() method to show the figure.Examplefrom matplotlib import pyplot as plt import numpy as np ... Read More
In this program, we will find the set difference of two numpy arrays. We will use the setdiff1d() function in the numpy library. This function takes two parameters: array1 and array2 and returns the unique values in array1 that are not in array2.AlgorithmStep 1: Import numpy. Step 2: Define two numpy arrays. Step 3: Find the set difference between these arrays using the setdiff1d() function. Step 4: Print the output.Example Codeimport numpy as np array_1 = np.array([2, 4, 6, 8, 10, 12]) print("Array 1: ", array_1) array_2 = np.array([4, 8, 12]) print("Array 2: ", array_2) set_diff = ... Read More
To convert the row values in a matrix to row percentage, we can find the row sums and divide each row value by this sum. For example, if we have a matrix called M then we can convert the row values in M to row percentage by using the commandround((M/rowSums(M))*100,2)ExampleConsider the below matrix − Live DemoM1
Whenever Y value list will be made, then we will convert those datasets into a new list, with ceil and floor value of the given list accordingly. Then, we can plot the graph for the new list data.StepsTake an input list.Find the minimum and maximum values in the input list (Step 1).Create a range between min and max value (Step 2).Get or set the current tick locations and labels of the Y-axis, with a new list.Set the X-axis label using plt.xlabel() method.Set the Y-axis label using plt.ylabel() method.Set a title for the axes.To show the figure we can use the ... Read More
In this problem, we will find the intersection between two numpy arrays. Intersection of two arrays is an array with elements common in both the original arraysAlgorithmStep 1: Import numpy. Step 2: Define two numpy arrays. Step 3: Find intersection between the arrays using the numpy.intersect1d() function. Step 4: Print the array of intersecting elements.Example Codeimport numpy as np array_1 = np.array([1,2,3,4,5]) print("Array 1:", array_1) array_2 = np.array([2,4,6,8,10]) print("Array 2:", array_2) intersection = np.intersect1d(array_1, array_2) print("The intersection between the two arrays is:", intersection)OutputArray 1: [1 2 3 4 5] Array 2: [2 4 6 8 10] The intersection between the two arrays is: [2 4]
pylab is a module that imports matplotlib.pyplot (for plotting) and numpy (for mathematics and working with arrays) in a single namespace.Although many examples use pylab, it is no longer recommended. For non-interactive plotting, it is suggested to use pyplot to create the figures and then the OO interface for plotting.Exampleimport matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2, 100) plt.plot(x, x, label='linear') plt.plot(x, x**2, label='quadratic') plt.plot(x, x**3, label='cubic') plt.xlabel('x label') plt.ylabel('y label') plt.title("Simple Plot") plt.legend() plt.show()Output