Python Articles

Page 515 of 855

Finding the multiples of a number in a given list using NumPy

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

Finding multiples of a number in a list is a common task in data analysis. NumPy provides efficient methods to identify multiples using vectorized operations and built-in functions like argwhere() and modulo operations. Using Basic Loop Method The traditional approach uses a loop to check each element ? import numpy as np listnum = np.arange(1, 20) multiples = [] n = 5 print("NumList:", listnum) for num in listnum: if num % n == 0: multiples.append(num) ...

Read More

How to calculate the frequency of each item in a Pandas series?

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

In this program, we will calculate the frequency of each element in a Pandas series. The function value_counts() in the pandas library helps us to find the frequency of elements. Algorithm Step 1: Define a Pandas series. Step 2: Print the frequency of each item using the value_counts() function. Example Code import pandas as pd series = pd.Series([10, 10, 20, 30, 40, 30, 50, 10, 60, 50, 50]) print("Series:", series) frequency = series.value_counts() print("Frequency of elements:", frequency) Output Series: 0 10 1 ...

Read More

How to get the nth percentile of a Pandas series?

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

A percentile is a statistical measure that indicates the value below which a certain percentage of observations fall. In Pandas, you can calculate the nth percentile of a series using the quantile() method. Syntax series.quantile(q) Parameters: q − The quantile value between 0 and 1 (percentile/100) Example Here's how to calculate the 50th percentile (median) of a Pandas series ? import pandas as pd # Create a Pandas series series = pd.Series([10, 20, 30, 40, 50]) print("Series:") print(series) # Calculate 50th percentile percentile_50 = series.quantile(0.5) print(f"The 50th ...

Read More

Python xticks in subplots

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

Subplots allow you to display multiple plots in a single figure by dividing it into a grid. When working with subplots, you can customize the x-axis ticks for each subplot independently using plt.xticks(). Understanding Subplot Layout The plt.subplot() function creates subplots using three parameters: nrows, ncols, and index. For example, plt.subplot(121) creates a 1×2 grid and selects the first subplot. Basic Subplot with Different X-ticks Here's how to create two subplots with custom x-tick positions − import matplotlib.pyplot as plt line1 = [21, 14, 81] line2 = [31, 6, 12] # First ...

Read More

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 444 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 523 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 395 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
Showing 5141–5150 of 8,546 articles
« Prev 1 513 514 515 516 517 855 Next »
Advertisements