AmitDiwan has Published 10744 Articles

Python program to Flatten Nested List to Tuple List

AmitDiwan

AmitDiwan

Updated on 21-Sep-2021 08:04:30

352 Views

When it is required to flatten a nested list into a tuple list, a method is defined that takes a list as a parameter, and uses the ‘isinstance’ method to check if an element belongs to a specific type. Depending on this, the output is displayed.ExampleBelow is a demonstration of ... Read More

Python - How to access the last element in a Pandas series?

AmitDiwan

AmitDiwan

Updated on 21-Sep-2021 08:01:38

777 Views

We will be using the iat attribute to access the last element, since it is used to access a single value for a row/column pair by integer position.Let us first import the required Pandas library −import pandas as pdCreate a Pandas series with numbers −data = pd.Series([10, 20, 5, 65, ... Read More

Python - Create nested list containing values as the count of list items

AmitDiwan

AmitDiwan

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

234 Views

When it is required to create a nested list containing values as the count of list elements, a simple iteration is used.ExampleBelow is a demonstration of the samemy_list = [11, 25, 36, 24] print("The list is :") print(my_list) for element in range(len(my_list)): my_list[element] = [element+1 for j ... Read More

Python - Count the frequency of matrix row length

AmitDiwan

AmitDiwan

Updated on 21-Sep-2021 07:58:38

220 Views

When it is required to count the frequency of the matrix row length, it is iterated over and its frequency is added to the empty dictionary or incremented if found again.ExampleBelow is a demonstration of the samemy_list = [[42, 24, 11], [67, 18], [20], [54, 10, 25], [45, 99]] ... Read More

Python - Character repetition string combinations

AmitDiwan

AmitDiwan

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

160 Views

When it is required to get the character repetitions of a given character, a method is defined that uses the index value to print the repetitions.ExampleBelow is a demonstration of the samedef to_string(my_list): return ''.join(my_list) def lex_recurrence(my_string, my_data, last_val, index_val): length = len(my_string) ... Read More

Python - Group contiguous strings in List

AmitDiwan

AmitDiwan

Updated on 21-Sep-2021 07:52:58

217 Views

When it is required to group the contiguous elements of a string that are present in a list, a method is defined that uses ‘groupby’, and ‘yield’.ExampleBelow is a demonstration of the samefrom itertools import groupby def string_check(elem):    return isinstance(elem, str) def group_string(my_list):       ... Read More

Python – Get the datatypes of columns

AmitDiwan

AmitDiwan

Updated on 21-Sep-2021 07:47:06

152 Views

To get the datatypes of columns, use the info() method. Let us first import the required library −import pandas as pdCreate a DataFrame with 2 columns having different datatypes −dataFrame = pd.DataFrame(    {       "Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'], "Roll Number": [ 5, 10, ... Read More

Python - Select columns with specific datatypes

AmitDiwan

AmitDiwan

Updated on 21-Sep-2021 07:45:55

198 Views

To select columns with specific datatypes, use the select_dtypes() method and the include parameter. At first, create a DataFrame with 2 columns −dataFrame = pd.DataFrame(    {       "Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'], "Roll Number": [ 5, 10, 3, 8, 2, 9, 6] ... Read More

Python - Add a zero column to Pandas DataFrame

AmitDiwan

AmitDiwan

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

2K+ Views

To add a zero column to a Pandas DataFrame, use the square bracket and set it to 0. At first, import te required library −import pandas as pdCreate a DataFrame with 3 columns −dataFrame = pd.DataFrame(    {       "Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'], "Result": ... Read More

Python - Element frequencies in percent range

AmitDiwan

AmitDiwan

Updated on 21-Sep-2021 07:19:57

313 Views

When it is required to find the element frequencies in the percentage range, the ‘Counter’ is used along with a simple iteration technique.ExampleBelow is a demonstration of the samefrom collections import Counter my_list = [56, 34, 78, 90, 11, 23, 6, 56, 79, 90] print("The list is :") ... Read More

Advertisements