AmitDiwan has Published 10744 Articles

Python program to convert a list into a list of lists using a step value

AmitDiwan

AmitDiwan

Updated on 15-Sep-2021 11:33:00

202 Views

When it is required to convert a list into a list of lists using a step value, a method is defined that uses a simple iteration, the ‘split’ method and the ‘append’ method.ExampleBelow is a demonstration of the samedef convert_my_list(my_list): my_result = [] for ... Read More

Python Program to find the cube of each list element

AmitDiwan

AmitDiwan

Updated on 15-Sep-2021 11:30:55

1K+ Views

When it is required to find the cube of each list element, a simple iteration and the ‘append’ method are used.ExampleBelow is a demonstration of the samemy_list = [45, 31, 22, 48, 59, 99, 0] print("The list is :") print(my_list) my_result = [] for i in my_list: ... Read More

Print all words occurring in a sentence exactly K times

AmitDiwan

AmitDiwan

Updated on 15-Sep-2021 11:25:55

378 Views

When it is required to print all the words occurring in a sentence exactly K times, a method is defined that uses the ‘split’ method, ‘remove’ method and the ‘count’ methods. The method is called by passing the required parameters and output is displayed.ExampleBelow is a demonstration of the samedef ... Read More

Python - Custom space size padding in Strings List

AmitDiwan

AmitDiwan

Updated on 15-Sep-2021 11:15:59

373 Views

When it is required to customize the space size padding in a list of strings, an empty list, an iteration and the ‘append’ method is used.ExampleBelow is a demonstration of the samemy_list = ["Python", "is", "great"] print("The list is :") print(my_list) lead_size = 3 trail_size = 2 ... Read More

Python - First occurrence of one list in another

AmitDiwan

AmitDiwan

Updated on 15-Sep-2021 11:13:46

361 Views

When it is required to find the first occurrence of one list in another list, the ‘set’ attribute and the ‘next’ method is used.ExampleBelow is a demonstration of the samemy_list_1 = [23, 64, 34, 77, 89, 9, 21] my_list_2 = [64, 10, 18, 11, 0, 21] print("The first list is ... Read More

Python - Merge Pandas DataFrame with Inner Join

AmitDiwan

AmitDiwan

Updated on 15-Sep-2021 09:55:17

1K+ Views

To merge Pandas DataFrame, use the merge() function. The inner join is implemented on both the DataFrames by setting under the “how” parameter of the merge() function i.e. −how = “inner”At first, let us import the pandas library with an alias −import pandas as pd Create DataFrame1 −dataFrame1 = pd.DataFrame( ... Read More

Python - Calculate the variance of a column in a Pandas DataFrame

AmitDiwan

AmitDiwan

Updated on 15-Sep-2021 09:43:21

1K+ Views

To calculate the variance of column values, use the var() method. At first, import the required Pandas library −import pandas as pdCreate a DataFrame with two columns −dataFrame1 = pd.DataFrame(    { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'],       "Units": [100, ... Read More

Python - How to reset index after Groupby pandas?

AmitDiwan

AmitDiwan

Updated on 15-Sep-2021 09:31:26

9K+ Views

To reset index after group by, at first group according to a column using groupby(). After that, use reset_index().At first, import the required library −import pandas as pdCreate a DataFrame with 2 columns −dataFrame = pd.DataFrame( { "Car": ["Audi", "Lexus", "Audi", ... Read More

Python - Sum only specific rows of a Pandas Dataframe

AmitDiwan

AmitDiwan

Updated on 15-Sep-2021 09:16:36

5K+ Views

To sum only specific rows, use the loc() method. Mention the beginning and end row index using the : operator. Using loc(), you can also set the columns to be included. We can display the result in a new column.At first, let us create a DataFrame. We have Product records ... Read More

Python - Calculate the median of column values of a Pandas DataFrame

AmitDiwan

AmitDiwan

Updated on 15-Sep-2021 09:06:09

677 Views

To calculate the median of column values, use the median() method. At first, import the required Pandas library −import pandas as pdNow, create a DataFrame with two columns −dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], ... Read More

Advertisements