
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 33676 Articles for Programming

922 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

294 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

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

266 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

530 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

231 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