Python Pandas - Generate dates in a range

AmitDiwan
Updated on 26-Mar-2026 02:15:24

850 Views

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 More

Python - Compute last of group values in a Pandas DataFrame

AmitDiwan
Updated on 26-Mar-2026 02:15:05

234 Views

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 More

Python Pandas - Filtering columns from a DataFrame on the basis of sum

AmitDiwan
Updated on 26-Mar-2026 02:14:47

971 Views

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 More

Python Pandas - Select first periods of time series data based on a date offset

AmitDiwan
Updated on 26-Mar-2026 02:14:27

255 Views

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 More

Python Pandas - Merge DataFrame with indicator value

AmitDiwan
Updated on 26-Mar-2026 02:14:10

5K+ Views

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 More

Python - Calculate the standard deviation of a column in a Pandas DataFrame

AmitDiwan
Updated on 26-Mar-2026 02:13:50

908 Views

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

Python Pandas - Select final periods of time series data based on a date offset

AmitDiwan
Updated on 26-Mar-2026 02:13:30

242 Views

To select final periods of time series based on a date offset, use the last() method. This method allows you to extract the most recent data points within a specified time window from a datetime-indexed DataFrame. Creating a Time Series DataFrame First, create a date range with specific periods and frequency ? import pandas as pd # Create date index with 5 periods and frequency of 3 days date_index = pd.date_range('2021-07-15', periods=5, freq='3D') print("Date Index:") print(date_index) Date Index: DatetimeIndex(['2021-07-15', '2021-07-18', '2021-07-21', '2021-07-24', ... Read More

Python Pandas – Remove leading and trailing whitespace from more than one column

AmitDiwan
Updated on 26-Mar-2026 02:13:10

1K+ Views

To remove leading and trailing whitespace from multiple columns in a Pandas DataFrame, use the str.strip() method. This is useful for cleaning data that contains unwanted spaces. Creating a DataFrame with Whitespace First, let's create a DataFrame with whitespace in the string columns ? import pandas as pd # Create a DataFrame with whitespace in string columns dataFrame = pd.DataFrame({ 'Product Category': [' Computer', ' Mobile Phone', 'Electronics ', 'Appliances', ' Furniture', 'Stationery'], 'Product Name': ['Keyboard', 'Charger', ' SmartTV', 'Refrigerators', ' Chairs', 'Diaries'], ... Read More

Compare specific Timestamps for a Pandas DataFrame – Python

AmitDiwan
Updated on 26-Mar-2026 02:12:46

871 Views

To compare specific timestamps in a Pandas DataFrame, you can access individual rows using index numbers and calculate the difference between timestamp columns. This is useful for analyzing time intervals between related events. Creating a DataFrame with Timestamps First, let's create a DataFrame containing timestamp data ? import pandas as pd # Create a DataFrame with timestamp columns dataFrame = pd.DataFrame({ "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW"], "Date_of_Purchase": [ pd.Timestamp("2021-06-10"), pd.Timestamp("2021-07-11"), ... Read More

Python Program to replace list elements within a range with a given number

AmitDiwan
Updated on 26-Mar-2026 02:12:26

1K+ Views

When working with Python lists, you may need to replace multiple elements within a specific range with the same value. This can be efficiently accomplished using list slicing combined with list multiplication. Syntax The basic syntax for replacing elements within a range is ? list[start:end] = [new_value] * (end - start) Where: start − starting index (inclusive) end − ending index (exclusive) new_value − the value to replace with Basic Example Here's how to replace elements at indices 4 to 7 with the number 9 ? numbers = ... Read More

Advertisements