AmitDiwan has Published 10744 Articles

Python Program to Remove Palindromic Elements from a List

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 08:12:02

352 Views

When it is required to remove palindromic elements from a list, list comprehension and the ‘not’ operator are used.ExampleBelow is a demonstration of the samemy_list = [56, 78, 12, 32, 4, 8, 9, 100, 11] print("The list is : ") print(my_list) my_result = [elem for elem in my_list ... Read More

Python – N sized substrings with K distinct characters

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 08:08:31

255 Views

When it is required to split ‘N’ sized substrings with ‘K’ distinct characters, it is iterated over, and the ‘set’ method is used to get the different combinations.ExampleBelow is a demonstration of the samemy_string = 'Pythonisfun' print("The string is : ") print(my_string) my_substring = 2 my_chars = 2 my_result ... Read More

Python Pandas - Count the number of rows in each group

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 08:05:51

355 Views

Use the group.size() to count the number of rows in each group. Import the required library −import pandas as pdCreate a DataFrame −dataFrame = pd.DataFrame({'Product Category': ['Computer', 'Mobile Phone', 'Electronics', 'Electronics', 'Computer', 'Mobile Phone'], 'Quantity': [10, 50, 10, 20, 25, 50], 'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Camera', 'Graphic Card', 'Earphone'] ... Read More

Python Pandas - Sort DataFrame in descending order according to the element frequency

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 07:58:37

713 Views

To sort data in ascending or descending order, use sort_values() method. For descending order, use the following in the sort_values() method −ascending=FalseImport the required library −import pandas as pd Create a DataFrame with 3 columns −dataFrame = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'BMW', 'Mustang', 'Mercedes', 'Lexus'], ... Read More

Python – Merge Dictionaries List with duplicate Keys

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 07:56:21

761 Views

When it is required to merge dictionaries list with duplicate keys, the keys of the strings are iterated over and depending on the condition, the result is determined.ExampleBelow is a demonstration of the samemy_list_1 = [{"aba": 1, "best": 4}, {"python": 10, "fun": 15}, {"scala": "fun"}] my_list_2 = [{"scala": 6}, ... Read More

Python – Remove Columns of Duplicate Elements

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 07:51:52

154 Views

When it is required to remove columns of duplicate elements, a method is defined that creates an empty set. The list is iterated over, and if it is not found, it is added to the set.ExampleBelow is a demonstration of the samefrom itertools import chain def remove_dupes(my_sub):   ... Read More

Python - Move a column to the first position in Pandas DataFrame?

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 07:48:45

1K+ Views

Use pop() to pop the column and insert it using the insert() methodi.e. moving a column. At first, create 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": ... Read More

Python – Element wise Matrix Difference

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 07:48:25

269 Views

When it is required to print element wise matrix difference, the list elements are iterated over and the zip method is used on these values.ExampleBelow is a demonstration of the samemy_list_1 = [[3, 4, 4], [4, 3, 1], [4, 8, 3]] my_list_2 = [[5, 4, 7], [9, 7, 5], [4, ... Read More

Python – Display only non-duplicate values from a DataFrame

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 07:42:04

4K+ Views

We will see how to display only non-duplicated values. At first, we will create a DataFrame with duplicate values −dataFrame = pd.DataFrame(    {       "Student": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'], "Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass'] } )Above, we have ... Read More

Python – Reshape the data in a Pandas DataFrame

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 07:34:11

772 Views

We can easily reshape the data by categorizing a specific column. Here, we will categorize the “Result”column i.e. Pass and Fail values in numbers form.Import the required library −import pandas as pdCreate a DataFrame with 2 columns −dataFrame = pd.DataFrame(    {       "Student": ['Jack', 'Robin', 'Ted', 'Scarlett', ... Read More

Advertisements