Find Common Columns Between Two DataFrames Using AND Operator in Pandas

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

274 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

Print Sorted Number Formed by Merging All Elements in Array

AmitDiwan
Updated on 21-Sep-2021 08:22:06

233 Views

When it is required to print the sorted numbers that are formed by merging the elements of an array, a method can be defined that first sorts the number and converts the number to an integer. Another method maps this list to a string, and is sorted again.ExampleBelow is a demonstration of the samedef get_sorted_nums(my_num): my_num = ''.join(sorted(my_num)) my_num = int(my_num) print(my_num) def merged_list(my_list): my_list = list(map(str, my_list)) my_str = ''.join(my_list) get_sorted_nums(my_str) my_list = [7, 845, 69, 60, ... Read More

Get All Unique Keys from a List of Dictionaries in Python

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

Print All Distinct Uncommon Digits in Two Given Numbers

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

226 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

Fetch Columns Between Two Pandas DataFrames by Intersection in Python

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

Get K-Length Groups with Given Summation in Python

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

212 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

Split Joined Consecutive Similar Characters in Python

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

300 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 Index Ranks of Elements

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

676 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

Append a List to a Pandas DataFrame Using append() in Python

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

832 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

Remove Non-Increasing Elements in Python

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

147 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

Advertisements