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 AmitDiwan
Page 100 of 840
Print all words occurring in a sentence exactly K times
When you need to print all words occurring in a sentence exactly K times, you can use Python's string methods like split(), count(), and remove(). This approach counts word frequency and displays words that match the specified frequency. Example Below is a demonstration of finding words with exact frequency ? def key_freq_words(my_string, K): my_list = list(my_string.split(" ")) for i in my_list: if my_list.count(i) == K: print(i) ...
Read MorePython - Custom space size padding in Strings List
When working with string lists in Python, you may need to add custom spacing around each string element. This can be achieved using string concatenation with multiplied spaces or built-in string methods like center(). Method 1: Using String Concatenation You can add custom leading and trailing spaces by multiplying space characters and concatenating them ? my_list = ["Python", "is", "great"] print("The list is:") print(my_list) lead_size = 3 trail_size = 2 my_result = [] for elem in my_list: my_result.append((lead_size * ' ') + elem + (trail_size * ' ')) print("The ...
Read MorePython - First occurrence of one list in another
When it is required to find the first occurrence of one list in another list, Python provides several approaches. The most efficient method uses the set data structure for fast lookups combined with the next() function for early termination. Using next() with Set Conversion This approach converts the second list to a set for O(1) lookup time and uses next() to return the first match ? my_list_1 = [23, 64, 34, 77, 89, 9, 21] my_list_2 = [64, 10, 18, 11, 0, 21] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) ...
Read MorePython - Merge Pandas DataFrame with Inner Join
To merge Pandas DataFrame, use the merge() function. An inner join returns only the rows that have matching values in both DataFrames. It's implemented by setting the how parameter to "inner". Syntax pd.merge(left, right, on='column_name', how='inner') How Inner Join Works An inner join combines rows from two DataFrames where the join condition is met. Only matching records from both DataFrames are included in the result. Creating Sample DataFrames Let's create two DataFrames with some common and different car models ? import pandas as pd # Create DataFrame1 dataFrame1 = ...
Read MorePython - Calculate the variance of a column in a Pandas DataFrame
To calculate the variance of column values in a Pandas DataFrame, use the var() method. Variance measures how spread out the data points are from the mean value. Syntax The basic syntax for calculating variance is ? DataFrame['column_name'].var() Creating a DataFrame First, import the required Pandas library and create a DataFrame ? import pandas as pd # Create DataFrame with car data dataFrame1 = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] }) print("DataFrame1:") ...
Read MorePython - How to reset index after Groupby pandas?
When you perform a groupby operation in pandas, the grouped column becomes the index. To convert this index back to a regular column, use reset_index(). Import Required Library First, import pandas ? import pandas as pd Creating Sample DataFrame Let's create a DataFrame with car names and prices ? import pandas as pd dataFrame = pd.DataFrame({ "Car": ["Audi", "Lexus", "Audi", "Mercedes", "Audi", "Lexus", "Mercedes", "Lexus", "Mercedes"], "Reg_Price": [1000, 1400, 1100, 900, 1700, 1800, 1300, 1150, 1350] }) print("Original DataFrame:") print(dataFrame) ...
Read MorePython - Sum only specific rows of a Pandas Dataframe
To sum only specific rows in a Pandas DataFrame, use the loc[] method to select specific row ranges and columns. The loc[] method allows you to specify both row indices and column names for precise data selection. Let's start by creating a DataFrame with product inventory data ? import pandas as pd # Create DataFrame with product inventory data dataFrame = pd.DataFrame({ "Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"], "Opening_Stock": [300, 700, 1200, 1500], "Closing_Stock": [200, 500, 1000, 900] }) print("Original DataFrame:") print(dataFrame) ...
Read MorePython Pandas – Find the common rows between two Data Frames
To find the common rows between two DataFrames in Pandas, use the merge() method with how='inner'. This returns only the rows that have identical values across all columns in both DataFrames. Creating Sample DataFrames Let us first create two DataFrames with car data ? import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] }) print("DataFrame1:") print(dataFrame1) DataFrame1: Car Units 0 ...
Read MorePython Pandas – Check if any specific column of two DataFrames are equal or not
To check if any specific column of two DataFrames are equal or not, use the equals() method. This method compares both the values and the structure of the columns, returning True if they are identical. Creating Sample DataFrames Let us first create DataFrame1 with two columns − import pandas as pd dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ) ...
Read MorePython - Calculate the mean of column values of a Pandas DataFrame
To calculate the mean of column values in a Pandas DataFrame, use the mean() method. This method computes the arithmetic average of numeric columns, making it essential for data analysis tasks. Basic Syntax The basic syntax for calculating column mean is ? # For a single column dataframe['column_name'].mean() # For all numeric columns dataframe.mean() Creating Sample DataFrames Let's create two DataFrames to demonstrate mean calculations ? import pandas as pd # Create DataFrame1 with car data dataFrame1 = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', ...
Read More