Found 26504 Articles for Server Side Programming

Python Pandas – Can we use & Operator to find common columns between two DataFrames?

AmitDiwan
Updated on 21-Sep-2021 08:23:20

266 Views

Yes, we can use the & operator to find the common columns between two DataFrames. At first, let us create two DataFrames −# creating dataframe1 dataFrame1 = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], }) print("Dataframe1...", dataFrame1) # creating dataframe2 dataFrame2 = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Units_Sold": [ 100, 110, 150, 80, 200, 90] })Get the common columns using the & operator −res = dataFrame1.columns & dataFrame2.columns ExampleFollowing is the code −import pandas as pd # creating dataframe1 dataFrame1 = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], ... Read More

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

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}, {'how' : 11, 'are' : 31}, {'you' : 28, 'Will':31}] print("The list is:") print(my_list) my_result = list(set(value for dic in my_list for value in dic.values())) print("The result is :") print(my_result)OutputThe list is: [{'there': 28, 'hi': 11}, {'how': 11, 'are': 31}, {'Will': 31, 'you': 28}] The result is : ... Read More

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

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

216 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 = str(val_1) val_2 = str(val_2) list_1 = list(map(int, val_1)) list_2 = list(map(int, val_2)) list_1 = set(list_1) list_2 = set(list_2) my_list = list_1.symmetric_difference(list_2) my_list = list(my_list) my_list.sort(reverse ... Read More

Python Program to get K length groups with given summation

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

203 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, 32] print("The list is : ") print(my_list) N = 77 print("The value of N is ") print(N) K = 2 print("The value of K is ") print(K) my_result = [] for sub in product(my_list, repeat = K): if sum(sub) == N: ... Read More

Python Program to Split joined consecutive similar characters

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

289 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 groupby(my_string)] print("The result is :") print(my_result)OutputThe original string is : pppyyytthhhhhhhooonnn The resultant split string is : ['ppp', 'yyy', 'tt', 'hhhhhhh', 'ooo', 'nnn']ExplanationThe required packages are imported into the environment.A string is defined and it is displayed on the console.The string is iterated over and it is sorted using ... Read More

Python - Fetch columns between two Pandas DataFrames by Intersection

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 = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Units_Sold": [ 100, 110, 150, 80, 200, 90] })Fetch common columns −dataFrame2.columns.intersection(dataFrame1.columns) ExampleFollowing is the complete code −import pandas as pd # creating dataframe1 dataFrame1 = pd.DataFrame({"Car": ['Bentley', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, ... Read More

Python - Index Ranks of Elements

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

661 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 samedef find_rank_elem(my_list): my_result = [0 for x in range(len(my_list))] for elem in range(len(my_list)): (r, s) = (1, 1) for j in range(len(my_list)): if ... Read More

Python - Remove non-increasing elements

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

137 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 my_list: if elem >= my_result[-1]: my_result.append(elem) print("The result is :") print(my_result)OutputThe list is : [5, 23, 45, 11, 45, 67, 89, 99, 10, 26, 7, 11] The result is : [5, 5, 23, 45, 45, 67, 89, 99] ... Read More

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

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

816 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, 75], ['New Zealand', 4 , 65], ['South Africa', 5, 50]] # Creating a DataFrame and adding columns dataFrame = pd.DataFrame(Team, columns=['Country', 'Rank', 'Points'])Let’s say the following is the row to be append −myList = [["Sri Lanka", 6, 40]] Append the above row in the form of list using append() ... Read More

Python - Consecutive Ranges of K greater than N

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

276 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 = 65 N = 3 print("The value of K is ") print(K) print("The value of N is ") print(N) my_result = [] beg, end = 0, 0 previous = 1 for index, element in enumerate(my_list): if element == K: end = ... Read More

Advertisements