Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Prasad Naik
Page 3 of 4
Print the mean of a Pandas series
The mean() function in the Pandas library can be used to find the arithmetic mean (average) of a series. This function calculates the sum of all values divided by the number of values. Syntax Series.mean(axis=None, skipna=True, level=None, numeric_only=None) Parameters The key parameters are: skipna: If True (default), excludes NaN values from calculation numeric_only: Include only numeric columns Example Here's how to calculate the mean of a Pandas series ? import pandas as pd series = pd.Series([10, 20, 30, 40, 50]) print("Pandas Series:") print(series) series_mean = ...
Read MoreHow to append elements to a Pandas series?
In Pandas, you can append elements to a Series using the append() method or the newer concat() function. The append() method allows you to combine two Series, but note that it's deprecated in newer Pandas versions in favor of concat(). Using append() Method The traditional approach uses the append() method to combine Series ? import pandas as pd s1 = pd.Series([10, 20, 30, 40, 50]) s2 = pd.Series([11, 22, 33, 44, 55]) print("S1:") print(s1) print("S2:") print(s2) appended_series = s1.append(s2) print("Final Series after appending:") print(appended_series) S1: 0 10 ...
Read MoreHow to sort a Pandas Series?
Sorting a Pandas Series is a common data manipulation task. The sort_values() method provides flexible options for arranging data in ascending or descending order while preserving the original index associations. Basic Sorting with sort_values() The sort_values() method sorts a Series by its values and returns a new sorted Series ? import pandas as pd # Create an unsorted Series numbers = pd.Series([18, 15, 66, 92, 55, 989]) print("Unsorted Pandas Series:") print(numbers) # Sort in ascending order (default) sorted_asc = numbers.sort_values() print("Sorted in Ascending Order:") print(sorted_asc) Unsorted Pandas Series: 0 ...
Read MoreHow to print array elements within a given range using Numpy?
In NumPy, you can print array elements within a specific range using several methods. The most common approaches are numpy.where() with numpy.logical_and(), boolean indexing, and conditional filtering. Using numpy.where() with logical_and() The numpy.where() function returns the indices of elements that meet a condition ? import numpy as np arr = np.array([1, 3, 5, 7, 10, 2, 4, 6, 8, 10, 36]) print("Original Array:") print(arr) # Find indices of elements between 4 and 20 (inclusive) indices = np.where(np.logical_and(arr >= 4, arr = 4) & (arr = 4) & (arr = min_val) & (arr
Read MoreHow to add a vector to a given Numpy array?
In this problem, we have to add a vector/array to a numpy array. We will define the numpy array as well as the vector and add them to get the result array using NumPy's broadcasting capabilities. Algorithm Step 1: Define a numpy array. Step 2: Define a vector. Step 3: Add vector to each row of the original array using broadcasting. Step 4: Print the result array. Method 1: Using Broadcasting (Recommended) NumPy automatically broadcasts the vector to each row ? import numpy as np original_array = np.array([[1, 2, 3], [4, ...
Read MoreHow to find the sum of rows and columns of a given matrix using Numpy?
In NumPy, you can calculate the sum of rows and columns of a matrix using the np.sum() function with the axis parameter. This is useful for data analysis and mathematical computations. Syntax numpy.sum(array, axis=None) Parameters: array − Input matrix or array axis − 0 for column-wise sum, 1 for row-wise sum Example Let's create a matrix and find the sum of rows and columns ? import numpy as np # Create a 2x2 matrix matrix = np.array([[10, 20], ...
Read MoreFinding the number of rows and columns in a given matrix using Numpy
NumPy provides several ways to find the dimensions of a matrix. The most common method is using the shape attribute, which returns a tuple containing the number of rows and columns. Creating a Matrix First, let's create a NumPy matrix to work with ? import numpy as np # Create a 2x3 matrix with random numbers matrix = np.random.rand(2, 3) print("Matrix:") print(matrix) Matrix: [[0.37454012 0.95071431 0.73199394] [0.59865848 0.15601864 0.15599452]] Finding Rows and Columns Using shape The shape attribute returns a tuple where the first element is the number ...
Read MoreHow to create an identity matrix using Numpy?
An identity matrix is a square matrix where diagonal elements are 1 and all other elements are 0. NumPy provides the identity() function to create identity matrices efficiently. Syntax numpy.identity(n, dtype=None) Parameters n: Size of the identity matrix (n x n) dtype: Data type of the matrix elements (optional, defaults to float) Creating a Basic Identity Matrix import numpy as np # Create a 3x3 identity matrix identity_matrix = np.identity(3) print(identity_matrix) [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]] Specifying Data Type ...
Read MoreWrite a Python program to remove a certain length substring from a given string
We need to write a Python program that removes a specific substring from a given string. Python provides several methods to accomplish this task efficiently. Algorithm Step 1: Define a string. Step 2: Use the replace() function to remove the substring from the given string. Step 3: Display the modified string. Using replace() Method The most straightforward approach is using the built-in replace() method to replace the unwanted substring with an empty string ? original_string = "C++ is a object oriented programming language" modified_string = original_string.replace("object oriented", "") print("Original:", original_string) print("Modified:", modified_string) ...
Read MorePrint dates of today, yesterday and tomorrow using Numpy
NumPy provides datetime functionality through the datetime64 data type, allowing you to easily work with dates. You can calculate today's, yesterday's, and tomorrow's dates using np.datetime64() and np.timedelta64() functions. Understanding DateTime64 The datetime64 function creates date objects, while timedelta64 represents time differences. The 'D' parameter specifies the unit as days − import numpy as np # Get today's date today = np.datetime64('today', 'D') print("Today's Date:", today) Today's Date: 2024-01-15 Calculating Yesterday and Tomorrow You can add or subtract timedelta64 objects to get past or future dates ? ...
Read More