Found 26504 Articles for Server Side Programming

Python – Check if elements index are equal for list elements

AmitDiwan
Updated on 13-Sep-2021 07:31:34

630 Views

When it is required to check if the index of the elements is equal to the elements in the list, a simple iteration and the enumerate attribute is used.ExampleBelow is a demonstration of the same −my_list_1 = [12, 62, 19, 79, 58, 0, 99] my_list_2 = [12, 74, 19, 54, 58, 0, 11] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_list_1.sort() my_list_2.sort() print("The first list after sorting is ") print(my_list_1) print("The second list after sorting is ") print(my_list_2) check_list = [9, 8, 2] print("The check_list is :") print(check_list) my_result ... Read More

Python – Convert List to Index and Value dictionary

AmitDiwan
Updated on 13-Sep-2021 07:28:01

685 Views

When it is required to convert a list into an index value dictionary, the ‘enumerate' and a simple iteration are used.ExampleBelow is a demonstration of the same −my_list = [32, 0, 11, 99, 223, 51, 67, 28, 12, 94, 89] print("The list is :") print(my_list) my_list.sort(reverse=True) print("The sorted list is ") print(my_list) index, value = "index", "values" my_result = {index : [], value : []} for id, vl in enumerate(my_list): my_result[index].append(id) my_result[value].append(vl) print("The result is :") print(my_result)OutputThe list is : [32, 0, 11, 99, 223, 51, 67, 28, 12, 94, ... Read More

Python – Extend consecutive tuples

AmitDiwan
Updated on 13-Sep-2021 07:24:47

146 Views

When it is required to extend consecutive tuples, a simple iteration is used.ExampleBelow is a demonstration of the same −my_list = [(13, 526, 73), (23, 67, 0, 72, 24, 13), (94, 42), (11, 62, 23, 12), (93, ), (83, 61)] print("The list is :") print(my_list) my_list.sort(reverse=True) print("The list after sorting in reverse is :") print(my_list) my_result = [] for index in range(len(my_list) - 1): my_result.append(my_list[index] + my_list[index + 1]) print("The result is :") print(my_result)OutputThe list is : [(13, 526, 73), (23, 67, 0, 72, 24, 13), (94, 42), (11, 62, 23, 12), (93, ... Read More

Python – Filter unique valued tuples

AmitDiwan
Updated on 13-Sep-2021 07:23:26

246 Views

When it is required to filter unique valued tuples from a list of tuples, the ‘list’ and ‘set’ methods are used.ExampleBelow is a demonstration of the same −my_list = [(42, 51), (46, 71), (14, 25), (26, 91), (56, 0), (11, 1), (99, 102)] print("The list of tuple is :") print(my_list) my_list.sort() print("The list after sorting is :") print(my_list) my_result = list(set(my_list)) print("The result is :") print(my_result)OutputThe list of tuple is : [(42, 51), (46, 71), (14, 25), (26, 91), (56, 0), (11, 1), (99, 102)] The list after sorting is : [(11, 1), (14, 25), (26, 91), (42, 51), (46, ... Read More

Python – 3D Matrix to Coordinate List

AmitDiwan
Updated on 13-Sep-2021 07:22:19

428 Views

When it is required to convert a three dimensional matrix into a co-ordinate list, the ‘zip’ method, and a list comprehension are used.ExampleBelow is a demonstration of the same −my_list_1 = [[['He', 'Wi'], ['llo', 'll']], [['Pyt', 'i'], ['hon', 'sFun']], [['Ho', 'g'], ['pe', 'ood']]] print("The list is : ") print(my_list_1) my_list_1.sort() print("The list after sorting is ") print(my_list_1) my_result = [ele for sub_elem_1, sub_elem_2 in my_list_1 for ele in zip(sub_elem_1, sub_elem_2)] print("The resultant list is : ") print(my_result)OutputThe list is : [[['He', 'Wi'], ['llo', 'll']], [['Pyt', 'i'], ['hon', 'sFun']], [['Ho', 'g'], ['pe', 'ood']]] The list after sorting is [[['He', ... Read More

Python – Cross Pairing in Tuple List

AmitDiwan
Updated on 13-Sep-2021 07:19:29

296 Views

When it is required to perform cross pairing in a list of tuples, the ‘zip’ method, a list comprehension and the ‘==’ operator is used.ExampleBelow is a demonstration of the same −my_list_1 = [('Hi', 'Will'), ('Jack', 'Python'), ('Bill', 'Mills'), ('goodwill', 'Jill')] my_list_2 = [('Hi', 'Band'), ('Jack', 'width'), ('Bill', 'cool'), ('a', 'b')] print("The first list is : " ) print(my_list_1) print("The second list is :") print(my_list_2) my_list_1.sort() my_list_2.sort() print("The first list after sorting is ") print(my_list_1) print("The second list after sorting is ") print(my_list_2) my_result = [(a[1], b[1]) for a, b in zip(my_list_1, my_list_2) if a[0] == b[0]] print("The ... Read More

Python – Sort grouped Pandas dataframe by group size?

SaiKrishna Tavva
Updated on 09-Oct-2024 14:28:13

9K+ Views

To group Pandas data frame, we use groupby(). To sort grouped data frames in ascending or descending order, use sort_values(). The size() method is used to get the data frame size. Steps Involved The steps included in sorting the panda's data frame by its group size are as follows. Importing the panda's library and Creating a Pandas dataframe. Grouping the columns by using the groupby() function and sorting the ... Read More

Python - Grouping columns in Pandas Dataframe

AmitDiwan
Updated on 09-Sep-2021 14:23:47

505 Views

To group columns in Pandas dataframe, use the groupby(). At first, let us create Pandas dataframe −dataFrame = pd.DataFrame(    {       "Car": ["Audi", "Lexus", "Audi", "Mercedes", "Audi", "Lexus", "Mercedes", "Lexus", "Mercedes"],       "Reg_Price": [1000, 1400, 1100, 900, 1700, 1800, 1300, 1150, 1350]    } )Let us now group according to Car column −res = dataFrame.groupby("Car")After grouping, we will use functions to find the means Registration prices (Reg_Price) of grouped car names −res.mean()This calculates mean of the Registration price according to column Car.ExampleFollowing is the code −import pandas as pd # dataframe with one of ... Read More

Python Pandas - Display the index of dataframe in the form of multi-index

AmitDiwan
Updated on 09-Sep-2021 14:14:30

283 Views

To display the index of dataframe in the form of multiindex, use the dataframe.index(). At first, let us create a dictionary of lists −# dictionary of lists d = {'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], 'Date_of_purchase': ['2020-10-10', '2020-10-12', '2020-10-17', '2020-10-16', '2020-10-19', '2020-10-22'] }Create a DataFrame from the above dictionary of lists −dataFrame = pd.DataFrame(d)Now, set index column “Car” and display the index −dataFrame.set_index(["Car"], inplace=True, append=True, drop=False) print"Multiindex...", dataFrame.indexExampleFollowing is the code −import pandas as pd # dictionary of lists d = {'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], 'Date_of_purchase': ['2020-10-10', ... Read More

Python - How to Group Pandas DataFrame by Days?

AmitDiwan
Updated on 09-Sep-2021 14:44:45

2K+ Views

We will group Pandas DataFrame using the groupby(). Select the column to be used using the grouper function. We will group day-wise and calculate sum of Registration Price with day interval for our example shown below for Car Sale Records.Set the frequency as an interval of days in the groupby() grouper method, that means, if the freq is 7D, that would mean data grouped by interval of 7 days of every month till the last date given in the date column.At first, let’s say the following is our Pandas DataFrame with three columns −import pandas as pd # dataframe ... Read More

Advertisements