Programming Articles

Page 413 of 2547

Comparing two Pandas series and printing the the difference

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 3K+ Views

In this article, we will compare two Pandas series and print the differences between them. By difference, we mean the index positions where elements did not match, along with the actual values from both series. What is Series Comparison? Pandas provides the compare() method to identify differences between two series. This method returns a DataFrame showing only the positions where values differ, with columns representing each series. Basic Example Let's start with a simple comparison between two series ? import pandas as pd s1 = pd.Series([10, 20, 30, 40, 50, 60]) s2 = ...

Read More

Print the standard deviation of Pandas series

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 446 Views

In this program, we will find the standard deviation of a Pandas series. Standard deviation is a statistic that measures the dispersion of a dataset relative to its mean and is calculated as the square root of the variance. Syntax Series.std(axis=None, skipna=True, level=None, ddof=1, numeric_only=None) Parameters The std() method accepts several parameters: ddof − Delta Degrees of Freedom (default is 1) skipna − Exclude NaN values (default is True) axis − Not applicable for Series Example Let's calculate the standard deviation of a Pandas series using the std() function: ...

Read More

Print the mean of a Pandas series

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 1K+ Views

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 More

How to append elements to a Pandas series?

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 16K+ Views

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 More

Pandas timeseries plot setting X-axis major and minor ticks and labels

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 524 Views

When working with Pandas time series data, you often need to customize the X-axis ticks and labels for better visualization. This involves setting both major and minor ticks to display dates at appropriate intervals. Steps Create a random number generator with a fixed seed for reproducible results. Generate a fixed frequency DatetimeIndex using pd.date_range() from '2020-01-01' to '2021-01-01'. Create sample data using a mathematical function or random distribution. Build a DataFrame with the time series data. Create a plot with custom figure size and configure major/minor ticks. Display the plot using plt.show(). Basic Time Series ...

Read More

How to sort a Pandas Series?

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 396 Views

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 More

How to print array elements within a given range using Numpy?

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 929 Views

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 More

How to add a vector to a given Numpy array?

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 3K+ Views

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 More

How to find the sum of rows and columns of a given matrix using Numpy?

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 4K+ Views

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 More

What's the fastest way of checking if a point is inside a polygon in Python?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 5K+ Views

Checking if a point is inside a polygon is a common computational geometry problem. Python offers several approaches, with matplotlib's Path class being one of the fastest and most reliable methods for this task. Using matplotlib.path for Point-in-Polygon Testing The matplotlib library provides an efficient implementation through the mplPath.Path class, which uses optimized algorithms for point-in-polygon testing. Steps Create a list of points to define the polygon vertices. Create a path object using mplPath.Path() with the polygon coordinates. Use the contains_point() method to check if a point lies inside the polygon. Example ...

Read More
Showing 4121–4130 of 25,466 articles
« Prev 1 411 412 413 414 415 2547 Next »
Advertisements