AmitDiwan has Published 10744 Articles

Python Pandas - Create a subset by choosing specific values from columns based on indexes

AmitDiwan

AmitDiwan

Updated on 14-Sep-2021 07:49:39

403 Views

To create a subset by choosing specific values from columns based on indexes, use the iloc() method. Let us first import the pandas libraryimport pandas as pdCreate a Pandas DataFrame with Product records. We have 3 columns in itdataFrame = pd.DataFrame({"Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"], "Opening_Stock": [300, 700, 1200, 1500], ... Read More

Python – Assign Alphabet to each element

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 11:52:58

410 Views

When it is required to assign an alphabet to every element of an integer list, the ‘ascii_lowercase’ method, and the list comprehension are used.ExampleBelow is a demonstration of the same −import string my_list = [11, 51, 32, 45, 21, 66, 12, 58, 90, 0] print("The list is : ... Read More

Python – Dictionaries with Unique Value Lists

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 11:51:39

164 Views

When it is required to get the dictionaries with unique value lists, the ‘set’ operator and the list methods are used, along with a simple iteration.ExampleBelow is a demonstration of the same −my_dictionary = [{'Python' : 11, 'is' : 22}, {'fun' : 11, 'to' : 33}, {'learn' : 22}, {'object':9}, ... Read More

Python – Get Matrix Mean

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 11:50:12

240 Views

When it is required to get the mean of the matrix elements, the ‘mean’ method from the ‘Numpy’ package is used after it has been imported into the environment.ExampleBelow is a demonstration of the same −import numpy as np my_matrix = np.matrix('[24, 41; 35, 25]') print("The matrix is : ... Read More

Python – Extract Key’s Value, if Key Present in List and Dictionary

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 11:49:16

556 Views

When it is required to extract the value of key if the key is present in the list as well as the dictionary, a simple iteration and the ‘all’ operator are used.ExampleBelow is a demonstration of the same −my_list = ["Python", "is", "fun", "to", "learn", "and", "teach", 'cool', 'object', 'oriented'] ... Read More

Python – Sort Dictionary List by Key’s ith Index value

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 11:47:30

452 Views

When it is required to sort the list of dictionary based on the key’s ‘i’th index value, the ‘sorted’ method and the lambda methods are used.ExampleBelow is a demonstration of the same −my_list = [{"Python" : "Best", "to" : "Code"}, {"Python" : "Good", "to" : "Learn"}, ... Read More

Python – Check if Splits are equal

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 11:45:31

267 Views

When it is required to check if the splits in a string are equal, the ‘len’ method, ‘list’ method and the ‘set’ operator are used along with an ‘if’ condition.ExampleBelow is a demonstration of the same −my_string = '96%96%96%96%96%96' print("The string is : " ) print(my_string) my_split_char = ... Read More

Python – Drop a level from a multi-level column index in Pandas dataframe

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 11:44:16

3K+ Views

To drop a level from a multi-level column index, use the columns.droplevel(). We have used the Multiindex.from_tuples() is used to create indexes column-wise.At first, create indexes column-wise −items = pd.MultiIndex.from_tuples([("Col 1", "Col 1", "Col 1"), ("Col 2", "Col 2", "Col 2"), ("Col 3", "Col 3", "Col 3")])Next, create a multiindex ... Read More

Python – Grouped Consecutive Range Indices of Elements

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 11:43:27

216 Views

When it is required to get the grouped consecutive range of indices of elements in a list, a defaultdict is created. A simple iteration, along with ‘groupby’ method, ‘len’ method, ‘list’ method and the ‘append’ methods are used.ExampleBelow is a demonstration of the same −from itertools import groupby from collections ... Read More

Python – Split Strings on Prefix Occurrence

AmitDiwan

AmitDiwan

Updated on 13-Sep-2021 11:39:00

426 Views

When it is required to split the strings based on the occurrence of the prefix, two empty lists are defined, and a prefix value is defined. A simple iteration is used along with ‘append’ method.ExampleBelow is a demonstration of the same −from itertools import zip_longest my_list = ["hi", 'hello', ... Read More

Advertisements