
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10476 Articles for Python

250 Views
When it is required to filter dictionaries by values in ‘K’th key in a list, a simple iteration by specifying the condition is used.ExampleBelow is a demonstration of the samemy_list = [{"Python": 2, "is": 4, "cool": 11}, {"Python": 5, "is": 1, "cool": 1}, {"Python": 7, "is": 3, "cool": 7}, {"Python": 9, "is": 9, "cool": 8}, {"Python": 4, "is": 10, "cool": 6}] print("The list is :") print(my_list) search_list = [1, 9, 8, 4, 5] key = "is" my_result = [] for sub in my_list: ... Read More

264 Views
When it is required to find the most common combination in a matrix, a simple iteration, along with the ‘sort’ method and ‘Counter’ method is used.ExampleBelow is a demonstration of the samefrom collections import Counter from itertools import combinations my_list = [[31, 25, 77, 82], [96, 15, 23, 32]] print("The list is :") print(my_list) my_result = Counter() for elem in my_list: if len(elem) < 2: continue elem.sort() for size in range(2, len(elem) + 1): for comb in combinations(elem, size): my_result[comb] += ... Read More

529 Views
To Groupby value counts, use the groupby(), size() and unstack() methods of the Pandas DataFrame. At first, create a DataFrame with 3 columns −dataFrame = pd.DataFrame({ 'Product Category': ['Computer', 'Mobile Phone', 'Electronics', 'Electronics', 'Computer', 'Mobile Phone'], 'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Camera', 'Graphic Card', 'Earphone'], 'Quantity': [10, 50, 10, 20, 25, 50]}) Now, groupby values count with groupby() method. For count, use the size() and unstack(). The unstack() gives a new level of column labels −dataFrame = dataFrame.groupby(['Product Category', 'Product Name', 'Quantity']).size().unstack(fill_value=0)ExampleFollowing is the complete code −import pandas as pd # create a dataframe with 3 columns ... Read More

610 Views
When it is required to get all pairwise combinations from a list, an iteration along with the ‘append’ method is used.ExampleBelow is a demonstration of the samemy_list = [15, "John", 2, "Will", 53, 'Rob'] print("The list is :") print(my_list) my_result = [] for i in range(0, len(my_list)): for j in range(0, len(my_list)): if (i!=j): my_result.append((my_list[i], my_list[j])) print("The result is :") print(my_result)OutputThe list is : [15, 'John', 2, 'Will', 53, 'Rob'] The result is : [(15, 'John'), (15, ... Read More

393 Views
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

230 Views
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

1K+ Views
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

270 Views
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

218 Views
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

479 Views
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