Python Articles

Page 356 of 855

Python program to remove words that are common in two Strings

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 984 Views

When working with text processing, you may need to remove words that are common between two strings and keep only the unique words. This can be achieved by counting word frequencies and filtering words that appear only once across both strings. Example Below is a demonstration of removing common words from two strings − def common_words_filter(string_1, string_2): word_count = {} # Count words from first string for word in string_1.split(): word_count[word] = word_count.get(word, 0) + ...

Read More

Python Program to Extract Elements from a List in a Set

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 454 Views

When it is required to extract elements from a list that exist in a set, a simple 'for' loop with membership checking can be used. This technique filters the list to keep only elements that are present in the target set. Method 1: Using For Loop Below is a demonstration using a basic for loop with conditional checking − my_list = [5, 7, 2, 7, 2, 4, 9, 8, 8] print("The list is:") print(my_list) search_set = {6, 2, 8} print("The search set is:") print(search_set) my_result = [] for element in my_list: ...

Read More

Python Pandas - Filling missing column values with median

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 3K+ Views

The median is a statistical measure that separates the higher half from the lower half of a dataset. In Pandas, you can fill missing values (NaN) in a DataFrame column with the median using the fillna() method combined with median(). Importing Required Libraries First, import Pandas and NumPy with their standard aliases ? import pandas as pd import numpy as np Creating DataFrame with Missing Values Create a DataFrame containing NaN values using np.NaN ? import pandas as pd import numpy as np # Create DataFrame with missing values dataFrame ...

Read More

Python program to mask a list using values from another list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 846 Views

When working with data analysis or filtering tasks, you often need to create a binary mask from one list based on values present in another list. Python provides several approaches to accomplish this task efficiently. What is List Masking? List masking creates a binary list (containing 0s and 1s) where 1 indicates the element exists in the reference list and 0 indicates it doesn't. This technique is commonly used in data filtering and boolean indexing. Using List Comprehension The most Pythonic approach uses list comprehension to create a mask ? my_list = [5, 6, ...

Read More

How to append a list to a Pandas DataFrame using loc in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 873 Views

The DataFrame.loc accessor is used to access a group of rows and columns by label or a boolean array. We can use it to append a list as a new row to an existing DataFrame by specifying the next available index position. Creating the Initial DataFrame Let us first create a DataFrame with team ranking data ? import pandas as pd # Data in the form of list of team rankings team_data = [['India', 1, 100], ['Australia', 2, 85], ['England', 3, 75], ...

Read More

Python Pandas - Filling missing column values with mode

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 3K+ Views

Mode is the value that appears most frequently in a dataset. In Pandas, you can fill missing values with the mode using the fillna() method combined with mode(). This is useful when you want to replace NaN values with the most common value in a column. Syntax dataframe.fillna(dataframe['column'].mode()[0], inplace=True) Creating DataFrame with Missing Values Let's start by importing the required libraries and creating a DataFrame with some missing values − import pandas as pd import numpy as np # Create DataFrame with NaN values dataFrame = pd.DataFrame({ ...

Read More

Python - Search DataFrame for a specific value with pandas

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 2K+ Views

Searching a DataFrame for specific values is a common operation in data analysis. Pandas provides several methods to locate and retrieve rows based on specific criteria using boolean indexing and iloc. Creating the DataFrame First, let's create a sample DataFrame with car information ? import pandas as pd # Creating DataFrame with car data dataFrame = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], "Units_Sold": ...

Read More

How to draw the largest polygon from a set of points in matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 613 Views

To draw the largest polygon from a set of points in matplotlib, we need to find the convex hull of the points and then create a polygon patch. The convex hull gives us the outermost boundary that encloses all points, forming the largest possible polygon. Understanding Convex Hull A convex hull is the smallest convex polygon that contains all given points. Think of it as stretching a rubber band around all the points − the shape it forms is the convex hull. Finding the Largest Polygon We'll use scipy's ConvexHull to find the largest polygon from ...

Read More

How to change the face color of a plot using Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 7K+ Views

Matplotlib allows you to customize the background color of your plots using the set_facecolor() method. This is useful for creating visually appealing plots or matching specific design requirements. Basic Face Color Change The simplest way to change the face color is using the set_facecolor() method on the axes object ? import matplotlib.pyplot as plt import numpy as np # Create data points x = np.linspace(-10, 10, 100) y = np.sin(x) # Create figure and axes fig, ax = plt.subplots(figsize=(8, 4)) # Plot the data ax.plot(x, y, color='yellow', linewidth=3) # Set the face ...

Read More

Plotting a masked surface plot using Python, Numpy and Matplotlib

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 751 Views

A masked surface plot allows you to hide or display only specific portions of 3D surface data based on certain conditions. This is useful when you want to exclude invalid data points or highlight specific regions of your surface. Setting Up the Environment First, we need to import the required libraries and configure the plot settings ? import matplotlib.pyplot as plt import numpy as np # Set figure size and layout plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True Creating the Coordinate Grid We'll create a coordinate grid using meshgrid to define our ...

Read More
Showing 3551–3560 of 8,546 articles
« Prev 1 354 355 356 357 358 855 Next »
Advertisements