Found 10476 Articles for Python

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

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', ' Chairs', 'Diaries'], 'Quantity': [10, 50, 10, 20, 25, 50]})Removing whitespace from more than one column −dataFrame['Product Category'].str.strip() dataFrame['Product Name'].str.strip()ExampleFollowing is the complete code −import pandas as pd # create a dataframe with 3 columns dataFrame = pd.DataFrame({    'Product Category': [' Computer', ' Mobile Phone', 'Electronics ', 'Appliances', ... Read More

Python - To Convert Matrix to String

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

965 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 = ", ", " " my_result = my_list_2.join([my_list_1.join([str(elem) for elem in sub]) for sub in my_list]) print("The result is :") print(my_result)OutputThe list is : [[1, 22, 'python'], [22, 'is', 1], ['great', 1, 91]] The result is : 1, 22, python 22, is, 1 great, 1, 91ExplanationA list of list is defined ... Read More

Compare specific Timestamps for a Pandas DataFrame – Python

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

738 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"],       "Date_of_Purchase": [          pd.Timestamp("2021-06-10"),          pd.Timestamp("2021-07-11"),          pd.Timestamp("2021-06-25"),          pd.Timestamp("2021-06-29"),          pd.Timestamp("2021-03-20"),       ],       "Date_of_Service": [           pd.Timestamp("2021-11-05"),           pd.Timestamp("2021-12-03"), ... Read More

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

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

884 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, 8 my_key = 9 my_list[i:j] = [my_key] * (j - i) print("The result is:") print(my_list)OutputThe list is : [42, 42, 18, 73, 11, 28, 29, 0, 10, 16, 22, 53, 41] The result is: [42, 42, 18, 73, 9, 9, 9, 9, 10, 16, 22, 53, 41]ExplanationA list ... Read More

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

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

159 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 = dict(zip(list(set(my_list)), range(len(set(my_list))))) my_result = [my_ordered_dict[elem] for elem in my_list] print("The result is: ") print(my_result)OutputThe list is : [91, 42, 27, 39, 24, 45, 53] The result is: [0, 2, 6, 1, 5, 3, 4]ExplanationA list is defined and is displayed on the console.The unique elements of the ... Read More

Python - Filter Supersequence Strings

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

168 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 elem in substring)] print("The resultant string is :") print(my_result)OutputThe list is : ['Python', '/', 'is', 'alwaysgreat', 'to', 'learn'] The resultant string is : ['alwaysgreat']ExplanationA list is defined and is displayed on the console.A substring is defined.The list comprehension is used to iterate through the elements using the ‘all’ clause.This ... Read More

Python - Maximum difference across lists

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

458 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 :") print(my_list_2) my_result = max(abs(my_list_2[index] - my_list_1[index]) for index in range(0, len(my_list_1) - 1)) print("The maximum difference among the lists is :") print(my_result)OutputThe first list is : [7, 9, 1, 2, 7] The second list is : [6, 3, 1, 2, 1] The maximum difference ... Read More

Python - Remove positional rows

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

140 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 :") print(my_list) my_index_list = [1, 2, 5] for index in my_index_list[::-1]: my_list.pop(index) print("The output is :") print(my_list)OutputThe list is : [[31, 42, 2], [1, 73, 29], [51, 3, 11], [0, 3, 51], [17, 3, 21], [1, 71, 10], [0, 81, 92]] The output is : [[31, ... Read More

Python – Strip whitespace from a Pandas DataFrame

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

921 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': [' Computer', ' Mobile Phone', 'Electronics ', 'Appliances', ' Furniture', 'Stationery'], 'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Refrigerators', 'Chairs', 'Diaries'], 'Quantity': [10, 50, 10, 20, 25, 50]}) Removing whitespace from a single column “Product Category” −dataFrame['Product Category'].str.strip()ExampleFollowing is the complete code − import pandas as pd # create a dataframe ... Read More

Python program to compute the power by Index element in List

AmitDiwan
Updated on 15-Sep-2021 12:20:03

292 Views

When it is required to compute the power by index element in a list, the simple iteration along with the ‘**’ operator is used.ExampleBelow is a demonstration of the samemy_list = [62, 18, 12, 63, 44, 75] print("The list is :") print(my_list) my_result = [] for my_index, elem in enumerate(my_list): my_result.append(elem ** my_index) print("The result is :") print(my_result)OutputThe list is : [62, 18, 12, 63, 44, 75] The result is : [1, 18, 144, 250047, 3748096, 2373046875]ExplanationA list is defined and is displayed on the console.An empty list is defined.The list is iterated ... Read More

Advertisements