To calculate the count of column values, use the count() method. At first, import the required Pandas library −import pandas as pdCreate a DataFrame with two columns −dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )Finding count of "Units" column values using the count() function −print"Count of values of Units column from DataFrame1 = ", dataFrame1['Units'].count() In the same way, we have calculated the count from the 2nd DataFrame.ExampleFollowing is the ... Read More
When it is required to find unique values count of every key, an iteration along with the ‘append’ method is used.ExampleBelow is a demonstration of the samemy_list = [12, 33, 33, 54, 84, 16, 16, 16, 58] print("The list is :") print(my_list) filtered_list = [] elem_count = 0 for item in my_list: if item not in filtered_list: elem_count += 1 filtered_list.append(item) print("The result is :") print(elem_count)OutputThe list is : [12, 33, 33, 54, 84, 16, 16, 16, 58] The result ... Read More
When it is required to mark duplicate elements in a string, list comprehension along with the ‘count’ method is used.ExampleBelow is a demonstration of the samemy_list = ["python", "is", "fun", "python", "is", "fun", "python", "fun"] print("The list is :") print(my_list) my_result = [value + str(my_list[:index].count(value) + 1) if my_list.count(value) > 1 else value for index, value in enumerate(my_list)] print("The result is :") print(my_result)OutputThe list is : ['python', 'is', 'fun', 'python', 'is', 'fun', 'python', 'fun'] The result is : ['python1', 'is1', 'fun1', 'python2', 'is2', 'fun2', 'python3', 'fun3']ExplanationA list is defined and is displayed on the console.The list comprehension ... Read More
When it is required to index directory of elements in a list, list comprehension along with ‘set’ operator is used.ExampleBelow is a demonstration of the samemy_list = [81, 36, 42, 57, 68, 12, 26, 26, 38] print("The list is :") print(my_list) my_result = {key: [index for index, value in enumerate(my_list) if value == key] for key in set(my_list)} print("The result is :") print(my_result)OutputThe list is : [81, 36, 42, 57, 68, 12, 26, 26, 38] The result is : {36: [1], 68: [4], 38: [8], 42: [2], 12: [5], 81: [0], 57: [3], 26: [6, ... Read More
When it is required to convert a list to a customized overlapping nested list, an iteration along with the ‘append’ method can be used.ExampleBelow is a demonstration of the samemy_list = [31, 25, 36, 76, 73, 89, 91, 100] print("The list is :") print(my_list) my_step, my_size = 3, 4 my_result = [] for index in range(0, len(my_list), my_step): my_result.append(my_list[index: index + my_size]) print("The result is :") print(my_result)OutputThe list is : [31, 25, 36, 76, 73, 89, 91, 100] The result is : [[31, 25, 36, 76], [76, 73, 89, 91], [91, 100]]ExplanationA list ... Read More
When it is required to get indices of sign change in a list, a simple iteration along with ‘append’ method can be used.ExampleBelow is a demonstration of the samemy_list = [71, 24, -34, -25, -76, 87, 29, -60, 70, 8] print("The list is :") print(my_list) my_result = [] for index in range(0, len(my_list) - 1): if my_list[index] > 0 and my_list[index + 1] < 0 or my_list[index] < 0 and my_list[index + 1] < 0: my_result.append(index) print("The result is :") print(my_result)OutputThe list is : [71, 24, -34, -25, -76, 87, 29, -60, ... Read More
When it is required to restrict the tuples by frequency of first element’s value, a simple ‘if’ condition along with an iteration and ‘append’ method is used.ExampleBelow is a demonstration of the samemy_list = [(21, 24), (13, 42), (11, 23), (32, 43), (25, 56),(73, 84), (91, 40), (40, 83), (13, 27)] print("The list is :") print(my_list) my_key = 1 my_result = [] mems = dict() for sub in my_list: if sub[0] not in mems.keys(): mems[sub[0]] = 1 else: mems[sub[0]] += 1 if mems[sub[0]]
When it is required to find the starting index of all nested lists, a simple iteration along with the ‘append’ method is used.ExampleBelow is a demonstration of the samemy_list = [[51], [91, 22, 36, 44], [25, 25], [46, 67, 78, 82, 69, 29], [ 7, 5]] print("The list is :") print(my_list) my_result = [] my_len = 0 for sub in my_list: my_result.append(my_len) my_len += len(sub) print("The initial element indices are :") print(my_result)OutputThe list is : [[51], [91, 22, 36, 44], [25, 25], [46, 67, 78, 82, 69, 29], [7, 5]] The ... Read More
When it is required to check if a list contains particular digits, the ‘join’ method, and a simple iteration are used.ExampleBelow is a demonstration of the samemy_list = [415, 133, 145, 451, 154] print("The list is :") print(my_list) my_digits = [1, 4, 5, 3] digit_string = ''.join([str(ele) for ele in my_digits]) all_elems = ''.join([str(ele) for ele in my_list]) my_result = True for element in all_elems: for ele in element: if ele not in digit_string: my_result = False ... Read More
When it is required to find the total equal pairs in a list, the ‘set’ operator and the ‘//’ operator along with an iteration can be used.ExampleBelow is a demonstration of the samemy_list = [34, 56, 12, 32, 78, 99, 67, 34, 52, 78, 99, 10, 0, 11, 23, 9] print("The list is :") print(my_list) all_elems = set(my_list) my_result = 0 for elements in all_elems: my_result += my_list.count(elements) // 2 print("The total pairs are :") print(my_result)OutputThe list is : [34, 56, 12, 32, 78, 99, 67, 34, 52, 78, 99, 10, 0, 11, 23, 9] ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP