Found 26504 Articles for Server Side Programming

Python Pandas - Filtering few rows from a DataFrame on the basis of sum

AmitDiwan
Updated on 14-Sep-2021 14:29:22

565 Views

To filter few rows from DataFrame on the basis of sum, we have considered an example with Student Marks. We need to calculate the sum of a particular subject wherein the total is more than 200 i.e. the total of all 3 students in that particular subject is more than 200. In this way we can fiter our rows with total less than 200.At first, let us create a DataFrame with 3 columns i.e. records of 3 students −dataFrame = pd.DataFrame({'Jacob_Marks': [95, 90, 70, 85, 88], 'Ted_Marks': [60, 50, 65, 85, 70], 'Jamie_Marks': [77, 76, 60, 45, 50]})Filtering on the ... Read More

Python Pandas – Fetch the Common rows between two DataFrames with concat()

AmitDiwan
Updated on 14-Sep-2021 14:24:38

541 Views

To fetch the common rows between two DataFrames, use the concat() function. Let us create DataFrame1 with two columns −dataFrame1 = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'],       "Reg_Price": [1000, 1500, 1100, 800, 1100, 900] } )Create DataFrame2 with two columns −dataFrame2 = pd.DataFrame(    { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Reg_Price": [1200, 1500, 1000, 800, 1100, 1000] } )Finding common rows between two DataFrames with concat() −dfRes = pd.concat([dataFrame1, dataFrame2])Reset index −dfRes = dfRes.reset_index(drop=True)Groupby columns −dfGroup = dfRes.groupby(list(dfRes.columns))Getting the length of each row to calculate the count. If ... Read More

Python Program – Convert String to matrix having K characters per row

AmitDiwan
Updated on 13-Sep-2021 08:19:15

744 Views

When it is required to convert a string into a matrix that has ‘K’ characters per row, a method is defined that takes a string and a value for ‘K’. It uses a simple iteration, the modulus operator and the ‘append’ method.ExampleBelow is a demonstration of the same −print("Method definition begins") def convert_my_string(my_string, my_k): for index in range(len(my_string)): if index % my_k == 0: sub = my_string[index:index+my_k] my_list = [] ... Read More

Python Pandas - Check if the dataframe objects are equal or not

AmitDiwan
Updated on 14-Sep-2021 14:19:50

210 Views

To check if the DataFrame objects are equal, use the equals() method. At first, let us create DataFrame1 with two columns −dataFrame1 = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000] } )Create DataFrame2 with two columns dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000] } )To check if the DataFrame ... Read More

Python – To convert a list of strings with a delimiter to a list of tuple

AmitDiwan
Updated on 13-Sep-2021 08:14:05

331 Views

When it is required to convert a list of strings with a delimiter to a list of tuples, a K value is set, and list comprehension along with the ‘split’ method is used.ExampleBelow is a demonstration of the same −my_list = ["33-22", "13-44-81-39", "42-10-42", "36-56-90", "34-77-91"] print("The list is : " ) print(my_list) print("The sorted list is ") my_list.sort() print(my_list) K = "-" print("The value of K is ") print(K) my_result = [tuple(int(element) for element in sub.split(K)) for sub in my_list] print("The resultant list is : ") print(my_result)OutputThe list is : ['33-22', '13-44-81-39', '42-10-42', '36-56-90', '34-77-91'] ... Read More

Python – Descending Order Sort grouped Pandas dataframe by group size?

AmitDiwan
Updated on 13-Sep-2021 08:03:05

4K+ Views

To group Pandas dataframe, we use groupby(). To sort grouped dataframe in descending order, use sort_values(). The size() method is used to get the dataframe size.For descending order sort, use the following in sort_values() −ascending=FalseAt first, create a pandas dataframe −dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], "Reg_Price": [1000, 1400, 1000, 900, 1700, 900] } )Next, group according to Reg_Price column and sort in descending order −dataFrame.groupby('Reg_Price').size().sort_values(ascending=False) ExampleFollowing is the codeimport pandas as pd # dataframe with one of the columns as Reg_Price ... Read More

Python – Sort given list of strings by part the numeric part of string

AmitDiwan
Updated on 13-Sep-2021 07:49:18

981 Views

When it is required to sort a given list of strings based on a numeric part of the string, a method is defined that uses the regular expressions, the ‘map’ method and the ‘list’ method to display the result.ExampleBelow is a demonstration of the same −import re print("The regular expression package has been imported successfully.") def my_digit_sort(my_list): return list(map(int, re.findall(r'\d+', my_list)))[0] my_list = ["pyt23hon", "fu30n", "lea14rn", 'co00l', 'ob8uje3345t'] print("The list is : " ) print(my_list) my_list.sort(key=my_digit_sort) print("The list has been sorted based on the pre-defined method..") print("The resultant list is : ") ... Read More

Python – Substitute prefix part of List

AmitDiwan
Updated on 13-Sep-2021 07:39:39

191 Views

When it is required to substitute prefix part of a list, the ‘len’ method and the ‘:’ operator is used.ExampleBelow is a demonstration of the same −my_list_1 = [15, 44, 82] my_list_2 = [29, 77, 19, 44, 26, 18] print("The first list is : " ) print(my_list_1) print("The second list is : " ) print(my_list_2) print("The first list after sorting is :") my_list_1.sort() print(my_list_1) print("The first list after sorting is :") my_list_2.sort() print(my_list_2) my_result = my_list_1 + my_list_2[len(my_list_1) : ] print("The resultant list is : ") print(my_result)OutputThe first list is : [15, 44, 82] ... Read More

Python - Merge DataFrames of different length

AmitDiwan
Updated on 23-Feb-2022 13:20:27

603 Views

To merge dataframes of different length, we need to use the merge() method. Let’s say the following is our 1st DataFrame with length 4 −dataFrame1 = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Audi', 'Jaguar']    } ) print("DataFrame1 ...", dataFrame1) print("DataFrame1 length = ", len(dataFrame1))Following is our 2nd DataFrame with length 6 −dataFrame2 = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley']    } ) print("DataFrame2 ...", dataFrame2) print("DataFrame2 length = ", len(dataFrame2))Now, merge DataFrames using the merge() −mergedRes = dataFrame2.merge(dataFrame1, how='left')ExampleFollowing is the code −import pandas as pd # ... Read More

Python – Cross mapping of Two dictionary value lists

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

636 Views

When it is required to cross-map two dictionary valued lists, the ‘setdefault’ and ‘extend’ methods are used.ExampleBelow is a demonstration of the same −my_dict_1 = {"Python" : [4, 7], "Fun" : [8, 6]} my_dict_2 = {6 : [5, 7], 8 : [3, 6], 7 : [9, 8]} print("The first dictionary is : " ) print(my_dict_1) print("The second dictionary is : " ) print(my_dict_2) sorted(my_dict_1.items(), key=lambda e: e[1][1]) print("The first dictionary after sorting is ") print(my_dict_1) sorted(my_dict_2.items(), key=lambda e: e[1][1]) print("The second dictionary after sorting is ") print(my_dict_2) my_result = {} for key, value in my_dict_1.items(): ... Read More

Advertisements