AmitDiwan has Published 10744 Articles

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

AmitDiwan

AmitDiwan

Updated on 15-Sep-2021 13:17:10

194 Views

To select final periods of time series based on a date offset, use the last() method. At first, set the date index with periods and freq. Freq is for frequency −i = pd.date_range('2021-07-15', periods=5, freq='3D')Now, create a DataFrame with above index −dataFrame = pd.DataFrame({'k': [1, 2, 3, 4, 5]}, index=i) ... Read More

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

AmitDiwan

AmitDiwan

Updated on 15-Sep-2021 13:07:01

1K+ Views

To remove leading or trailing whitespace, use the strip() method. At first, create a DataFrame with 3 columns “Product Category”, “Product Name” and “Quantity” −dataFrame = pd.DataFrame({ 'Product Category': [' Computer', ' Mobile Phone', 'Electronics ', 'Appliances', ' Furniture', 'Stationery'], 'Product Name': ['Keyboard', 'Charger', ' SmartTV', 'Refrigerators', ... Read More

Python - To Convert Matrix to String

AmitDiwan

AmitDiwan

Updated on 15-Sep-2021 12:37:31

961 Views

When it is required to convert a matrix into a string, a simple list comprehension along with the ‘join’ method is used.ExampleBelow is a demonstration of the samemy_list = [[1, 22, "python"], [22, "is", 1], ["great", 1, 91]] print("The list is :") print(my_list) my_list_1, my_list_2 = ", ", ... Read More

Compare specific Timestamps for a Pandas DataFrame – Python

AmitDiwan

AmitDiwan

Updated on 15-Sep-2021 12:36:21

732 Views

To compare specific timestamps, use the index number in the square brackets. At first, import the required library −import pandas as pdCreate a DataFrame with 3 columns. We have two date columns with timestamp −dataFrame = pd.DataFrame(    {       "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW"],   ... Read More

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

AmitDiwan

AmitDiwan

Updated on 15-Sep-2021 12:35:37

877 Views

When it is required to replace list elements within a range with a given number, list slicing is used.ExampleBelow is a demonstration of the samemy_list = [42, 42, 18, 73, 11, 28, 29, 0, 10, 16, 22, 53, 41] print("The list is :") print(my_list) i, j = 4, ... Read More

Python Program to assign each list element value equal to its magnitude order

AmitDiwan

AmitDiwan

Updated on 15-Sep-2021 12:34:21

156 Views

When it is required to assign each list element value equal to its magnitude order, the ‘set’ operation, the ‘zip’ method and a list comprehension are used.ExampleBelow is a demonstration of the samemy_list = [91, 42, 27, 39, 24, 45, 53] print("The list is : ") print(my_list) my_ordered_dict ... Read More

Python – Strip whitespace from a Pandas DataFrame

AmitDiwan

AmitDiwan

Updated on 15-Sep-2021 12:28:27

919 Views

To strip whitespace, whether its leading or trailing, use the strip() method. At first, let us import thr required Pandas library with an alias −import pandas as pdLet’s create a DataFrame with 3 columns. The first column is having leading and trailing whitespaces −dataFrame = pd.DataFrame({    'Product Category': [' ... Read More

Python - Filter Supersequence Strings

AmitDiwan

AmitDiwan

Updated on 15-Sep-2021 12:27:07

166 Views

When it is required to filter supersequence strings, a simple list comprehension is used.ExampleBelow is a demonstration of the samemy_list = ["Python", "/", "is", "alwaysgreat", "to", "learn"] print("The list is :") print(my_list) substring = "ys" my_result = [sub for sub in my_list if all(elem in sub for ... Read More

Python - Maximum difference across lists

AmitDiwan

AmitDiwan

Updated on 15-Sep-2021 12:25:07

454 Views

When it is required to find the maximum difference across the lists, the ‘abs’ and the ‘max’ methods are used.ExampleBelow is a demonstration of the samemy_list_1 = [7, 9, 1, 2, 7] my_list_2 = [6, 3, 1, 2, 1] print("The first list is :") print(my_list_1) print("The second list is :") ... Read More

Python - Remove positional rows

AmitDiwan

AmitDiwan

Updated on 15-Sep-2021 12:22:58

138 Views

When it is required to remove positional rows, a simple iteration and the ‘pop’ method is used.ExampleBelow is a demonstration of the samemy_list = [[31, 42, 2], [1, 73, 29], [51, 3, 11], [0, 3, 51], [17, 3, 21], [1, 71, 10], [0, 81, 92]] print("The list is :") ... Read More

Advertisements