AmitDiwan has Published 10744 Articles

Python Program to get all unique keys from a List of Dictionaries

AmitDiwan

AmitDiwan

Updated on 21-Sep-2021 08:19:49

2K+ Views

When it is required to get all the unique keys from a list of dictionary, the dictionary values are iterated over and converted into a set. This is converted to a list and displayed on the console.ExampleBelow is a demonstration of the samemy_list = [{'hi' : 11, 'there' : 28}, ... Read More

Python Program to print all distinct uncommon digits present in two given numbers

AmitDiwan

AmitDiwan

Updated on 21-Sep-2021 08:18:23

214 Views

When it is required to print all the distinct uncommon digits that are present in two numbers, a method is defined that takes two integers as parameters. The method ‘symmetric_difference’ is used to get the uncommon digits.ExampleBelow is a demonstration of the samedef distinct_uncommon_nums(val_1, val_2): val_1 ... Read More

Python - Fetch columns between two Pandas DataFrames by Intersection

AmitDiwan

AmitDiwan

Updated on 21-Sep-2021 08:17:57

2K+ Views

To fetch columns between two DataFrames by Intersection, use the intersection() method. Let us create two DataFrames −# creating dataframe1 dataFrame1 = pd.DataFrame({"Car": ['Bentley', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], }) # creating dataframe2 dataFrame2 = ... Read More

Python Program to get K length groups with given summation

AmitDiwan

AmitDiwan

Updated on 21-Sep-2021 08:16:34

202 Views

When it is required to get ‘K’ length groups with a given summation, an empty list, the ‘product’ method, the ‘sum’ method and the ‘append’ method can be used.ExampleBelow is a demonstration of the samefrom itertools import product my_list = [45, 32, 67, 11, 88, 90, 87, 33, 45, ... Read More

Python Program to Split joined consecutive similar characters

AmitDiwan

AmitDiwan

Updated on 21-Sep-2021 08:14:57

287 Views

When it is required to split the joined consecutive characters that are similar in nature, the ‘groupby’ method and the ‘join’ method are used.ExampleBelow is a demonstration of the samefrom itertools import groupby my_string = 'pppyyytthhhhhhhoooooonnn' print("The string is :") print(my_string) my_result = ["".join(grp) for elem, grp in ... Read More

Python - Index Ranks of Elements

AmitDiwan

AmitDiwan

Updated on 21-Sep-2021 08:13:43

658 Views

When it is required to determine the index rank of elements in a data structure, a method is defined that takes a list as a parameter. It iteeates over the elements in the list, and performs certain comparisons before changing the values of two variables.ExampleBelow is a demonstration of the ... Read More

How to append a list to a Pandas DataFrame using append() in Python?

AmitDiwan

AmitDiwan

Updated on 21-Sep-2021 08:12:00

812 Views

To append a list to a DataFrame using append(), let us first create a DataFrame. The data is in the form of lists of team rankings for our example − # data in the form of list of team rankings Team = [['India', 1, 100], ['Australia', 2, 85], ['England', 3, ... Read More

Python - Remove non-increasing elements

AmitDiwan

AmitDiwan

Updated on 21-Sep-2021 08:11:50

136 Views

When it is required to remove non-increasing elements, a simple iteration is used along with comparison of elements.ExampleBelow is a demonstration of the samemy_list = [5, 23, 45, 11, 45, 67, 89, 99, 10, 26, 7, 11] print("The list is :") print(my_list) my_result = [my_list[0]] for elem in ... Read More

Python - Consecutive Ranges of K greater than N

AmitDiwan

AmitDiwan

Updated on 21-Sep-2021 08:10:07

274 Views

When it is required to get the consecutive ranges of ‘K’ which are greater than ‘N’, the ‘enumerate’ attribute and simple iteration is used.ExampleBelow is a demonstration of the samemy_list = [3, 65, 33, 23, 65, 65, 65, 65, 65, 65, 65, 3, 65] print("The list is :") print(my_list) K ... Read More

Python – Stacking a single-level column with Pandas stack()?

AmitDiwan

AmitDiwan

Updated on 21-Sep-2021 08:05:36

483 Views

To stack a single-level column, use the datafrem.stack(). At first, let us import the required library −import pandas as pdCreate a DataFrame with single-level column −dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35], [40, 45]], index=['w', 'x', 'y', 'z'], columns=['a', 'b'])Stack the DataFrame using the stack() method −dataFrame.stack() ExampleFollowing is ... Read More

Advertisements