To add a zero column to a Pandas DataFrame, use the square bracket and set it to 0. At first, import te required library −import pandas as pdCreate a DataFrame with 3 columns −dataFrame = pd.DataFrame( { "Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'], "Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass'], "Roll Number": [ 5, 10, 3, 8, 2, 9, 6] } )Create a new column with zero entries −dataFrame['ZeroColumn'] = 0 ExampleFollowing is the code −import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( { ... Read More
When it is required to find the element frequencies in the percentage range, the ‘Counter’ is used along with a simple iteration technique.ExampleBelow is a demonstration of the samefrom collections import Counter my_list = [56, 34, 78, 90, 11, 23, 6, 56, 79, 90] print("The list is :") print(my_list) start, end = 13, 60 my_freq = dict(Counter(my_list)) my_result = [] for element in set(my_list): percent = (my_freq[element] / len(my_list)) * 100 if percent >= start and percent
To open a list, we can use append() method. With that, we can also use loc() method. At first, let us import the required library −import pandas as pdFollowing is the data in the form of lists of team rankings −Team = [['India', 1, 100], ['Australia', 2, 85], ['England', 3, 75], ['New Zealand', 4 , 65], ['South Africa', 5, 50]]Creating a DataFrame with the above data and adding columns −dataFrame = pd.DataFrame(Team, columns=['Country', 'Rank', 'Points']) Let’s say the following is the row to be appended −myList = [["Sri Lanka", 6, 40]]Append the above row in the form of list − ... Read More
When it is required to get the specific number of power of a number, the ‘**’ operator is used along with list comprehension.ExampleBelow is a demonstration of the samen = 4 print("The value n is : ") print(n) k = 5 print("The value of k is : ") print(k) result = [n ** index for index in range(0, k)] print("The square values of N till K : " ) print(result)OutputThe value n is : 4 The value of k is : 5 The square values of N till K : [1, 4, 16, 64, 256]ExplanationThe values for ‘n’ ... Read More
When it is required to find duplicate sets in a list of sets, the ‘Counter’ and ‘frozenset’ are used.ExampleBelow is a demonstration of the samefrom collections import Counter my_list = [{4, 8, 6, 1}, {6, 4, 1, 8}, {1, 2, 6, 2}, {1, 4, 2}, {7, 8, 9}] print("The list is :") print(my_list) my_freq = Counter(frozenset(sub) for sub in my_list) my_result = [] for key, value in my_freq.items(): if value > 1 : my_result.append(key) print("The result is :") print(my_result)OutputThe list is : [{8, 1, 4, 6}, {8, 1, ... Read More
When it is required to add custom borders to the matrix, a simple list iteration can be used to add the required borders to the matrix.ExampleBelow is a demonstration of the samemy_list = [[2, 5, 5], [2, 7, 5], [4, 5, 1], [1, 6, 6]] print("The list is :") print(my_list) print("The resultant matrix is :") border = "|" for sub in my_list: my_temp = border + " " for ele in sub: my_temp = my_temp + str(ele) + " " ... Read More
When it is required to remove words that are common in both the strings, a method is defined that takes two strings. The strings are spit based on spaces and list comprehension is used to filter out the result.ExampleBelow is a demonstration of the samedef common_words_filter(my_string_1, my_string_2): my_word_count = {} for word in my_string_1.split(): my_word_count[word] = my_word_count.get(word, 0) + 1 for word in my_string_2.split(): my_word_count[word] = my_word_count.get(word, 0) + 1 return [word for ... Read More
To concatenate DataFrames, use the concat() method, but to ignore duplicates, use the drop_duplicates() method.Import the required library −import pandas as pdCreate DataFrames to be concatenated −# Create DataFrame1 dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Jaguar', 'Audi', 'Mustang'], "Units": [100, 150, 110, 80] } ) # Create DataFrame2 dataFrame2 = pd.DataFrame( { "Car": ['Tesla', 'Jaguar', 'Mercedes', 'Mustang'], "Units": [120, 150, 180, 80] } ) Now, let us concatenate DataFrame and remove duplicates −concatRes = pd.concat([dataFrame1, dataFrame2]).drop_duplicates()ExampleFollowing is the code −import pandas as pd # ... Read More
When it is required to extract elements from a List in a Set, a simple ‘for’ loop and a base condition can be used.ExampleBelow is a demonstration of the samemy_list = [5, 7, 2, 7, 2, 4, 9, 8, 8] print("The list is :") print(my_list) search_set = {6, 2, 8} my_result = [] for element in my_list: if element in search_set: my_result.append(element) print("The result is :") print(my_result)OutputThe list is : [5, 7, 2, 7, 2, 4, 9, 8, 8] The result is : [2, 2, 8, ... Read More
When it is required to mask a list with the help of values from another list, list comprehension is used.ExampleBelow is a demonstration of the samemy_list = [5, 6, 1, 9, 11, 0, 4] print("The list is :") print(my_list) search_list = [2, 10, 6, 3, 9] result = [1 if element in search_list else 0 for element in my_list] print("The result is :") print(result)OutputThe list is : [5, 6, 1, 9, 11, 0, 4] The result is : [0, 1, 0, 1, 0, 0, 0]ExplanationA list is defined and is displayed on the console.Another list of ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP