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 96 of 840
Python program to randomly create N Lists of K size
When you need to generate multiple random lists of a specific size from a larger dataset, Python's random.shuffle() combined with generator functions provides an efficient solution. This approach creates N lists, each containing K randomly selected elements. Using Generator Function with Shuffle Here's how to create a generator that produces random sublists ? from random import shuffle def gen_random_list(my_val, K): while True: shuffle(my_val) yield my_val[:K] my_list = [12, 45, 76, 32, 45, 88, 99, ...
Read MorePython - Count distinct in Pandas Aggregation with Numpy
To count distinct values in Pandas aggregation, use nunique() method. This tutorial shows how to group by a column, calculate sums using NumPy, and count distinct values simultaneously. Required Libraries Import the necessary libraries for data manipulation ? import pandas as pd import numpy as np Creating Sample DataFrame Create a DataFrame with duplicate values to demonstrate distinct counting ? import pandas as pd import numpy as np dataFrame = pd.DataFrame({ "Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Lexus'], "Place": ['Delhi', 'Bangalore', 'Delhi', 'Chandigarh', ...
Read MorePython - Remove duplicate values from a Pandas DataFrame
To remove duplicate values from a Pandas DataFrame, use the drop_duplicates() method. This method identifies rows with identical values across all columns and removes the duplicates, keeping only the first occurrence of each unique row. Creating a DataFrame with Duplicates Let's create a sample DataFrame containing duplicate rows ? import pandas as pd # Create DataFrame with duplicate rows dataFrame = pd.DataFrame({ 'Car': ['BMW', 'Mercedes', 'Lamborghini', 'BMW', 'Mercedes', 'Porsche'], 'Place': ['Delhi', 'Hyderabad', 'Chandigarh', 'Delhi', 'Hyderabad', 'Mumbai'], 'UnitsSold': [95, 70, 80, 95, 70, 90] ...
Read MorePython – Group and calculate the sum of column values of a Pandas DataFrame
In Pandas, you can group DataFrame rows by specific columns and calculate aggregated values like sum, mean, or count. This is particularly useful for analyzing time-series data where you want to group by periods like months, quarters, or years. Creating a Sample DataFrame Let's create a DataFrame with car sales data to demonstrate grouping and summing ? import pandas as pd # Create DataFrame with car sales data dataFrame = pd.DataFrame({ "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW", "Toyota", "Nissan", "Bentley", "Mustang"], "Date_of_Purchase": [ ...
Read MorePython Pandas - Generate dates in a range
Pandas date_range() function generates a sequence of dates within a specified range. This is useful for creating time series data, filtering datasets by date periods, or setting up date indexes for analysis. Syntax pd.date_range(start, end, periods, freq) Parameters The key parameters are: start − Starting date of the range end − Ending date of the range periods − Number of dates to generate freq − Frequency (default is 'D' for daily) Generating Dates with ...
Read MorePython - Compute last of group values in a Pandas DataFrame
The groupby().last() method in Pandas returns the last row of each group when data is grouped by one or more columns. This is useful for getting the most recent entry for each category in your dataset. Creating Sample Data First, let's create a DataFrame with some sample car sales data ? import pandas as pd dataFrame = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'BMW', 'Tesla', 'Lexus', 'Tesla'], "Place": ['Delhi', 'Bangalore', 'Pune', 'Punjab', 'Chandigarh', 'Mumbai'], "Units": [100, 150, 50, 80, 110, 90] }) print("Original DataFrame:") ...
Read MorePython Pandas - Filtering columns from a DataFrame on the basis of sum
In Pandas, you can filter DataFrame columns based on their sum values using the loc[] indexer with conditional logic. This technique is useful when you want to select only columns that meet certain aggregate criteria. Creating the DataFrame First, let's create a sample DataFrame with student marks ? import pandas as pd # Create a DataFrame with student marks df = pd.DataFrame({ 'Jacob_Marks': [95, 90, 75, 85, 88], 'Ted_Marks': [60, 50, 65, 85, 70], 'Jamie_Marks': [77, 76, 65, 45, 50] }) print("Original ...
Read MorePython Pandas - Select first periods of time series data based on a date offset
To select first periods of time series based on a date offset, use the first() method. This method filters rows from the beginning of a DataFrame up to a specified time period. Creating a Time Series DataFrame First, create a date range index with specific periods and frequency ? import pandas as pd # Create date index with 5 periods and frequency of 3 days i = pd.date_range('2021-07-15', periods=5, freq='3D') # Create DataFrame with date index dataFrame = pd.DataFrame({'k': [1, 2, 3, 4, 5]}, index=i) print("DataFrame...") print(dataFrame) DataFrame... ...
Read MorePython Pandas - Merge DataFrame with indicator value
To merge Pandas DataFrame with indicator information, use the merge() function with the indicator parameter set to True. This adds a special _merge column showing the source of each row. What is the Indicator Parameter? The indicator parameter creates a column that tracks whether each row comes from the left DataFrame, right DataFrame, or both ? import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] }) print("DataFrame1:") print(dataFrame1) ...
Read MorePython - Calculate the standard deviation of a column in a Pandas DataFrame
Standard deviation measures how spread out values are from the mean. In Pandas, you can calculate the standard deviation of a DataFrame column using the std() method. Syntax To calculate standard deviation of a specific column ? dataframe['column_name'].std() Creating Sample DataFrames First, let's create sample DataFrames with numerical data ? import pandas as pd # Create DataFrame1 with car sales data dataFrame1 = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] }) print("DataFrame1:") print(dataFrame1) ...
Read More