AmitDiwan has Published 10744 Articles

Python - Convert one datatype to another in a Pandas DataFrame

AmitDiwan

AmitDiwan

Updated on 16-Sep-2021 08:55:23

391 Views

Use the astype() method in Pandas to convert one datatype to another. Import the required library −import pandas as pdCreate a DataFrame. Here, we have 2 columns, “Reg_Price” is a float type and “Units” int type −dataFrame = pd.DataFrame( { "Reg_Price": ... Read More

Python – Extract Paired Rows

AmitDiwan

AmitDiwan

Updated on 16-Sep-2021 08:52:55

166 Views

When it is required to extract paired rows, a list comprehension and the ‘all’ operator is used.ExampleBelow is a demonstration of the samemy_list = [[10, 21, 34, 21, 37], [41, 41, 52, 68, 68, 41], [12, 29], [30, 30, 51, 51]] print("The list is :") print(my_list) my_result = ... Read More

Python – All replacement combination from other list

AmitDiwan

AmitDiwan

Updated on 16-Sep-2021 08:51:09

282 Views

When it is required to get the replacement combination from the other list, the ‘combinations’ method and the ‘list’ method is used.ExampleBelow is a demonstration of the samefrom itertools import combinations my_list = [54, 98, 11] print("The list is :") print(my_list) replace_list = [8, 10] my_result ... Read More

Python - Sort rows by Frequency of K

AmitDiwan

AmitDiwan

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

165 Views

When it is required to sort the rows by frequency of ‘K’, a list comprehension and ‘Counter’ methods are used.ExampleBelow is a demonstration of the samefrom collections import Counter my_list = [34, 56, 78, 99, 99, 99, 99, 99, 12, 12, 32, 51, 15, 11, 0, 0] print ... Read More

Python – Center align column headers of a Pandas DataFrame

AmitDiwan

AmitDiwan

Updated on 16-Sep-2021 08:48:12

6K+ Views

To center align column headers, use the display.colheader_justify and ‘center’ value. Import the require library −import pandas as pdCreate a DataFrame with 2 columns −dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], ... Read More

Python - Selective consecutive Suffix Join

AmitDiwan

AmitDiwan

Updated on 16-Sep-2021 08:47:24

147 Views

When it is required to find the selective consecutive suffix join, a simple iteration, the ‘endswith’ method and the ‘append’ method can be used.ExampleBelow is a demonstration of the samemy_list = ["Python-", "fun", "to-", "code"] print("The list is :") print(my_list) suffix = '-' print("The suffix is :") print(suffix) ... Read More

Python Program that print elements common at specified index of list elements

AmitDiwan

AmitDiwan

Updated on 16-Sep-2021 08:44:53

476 Views

When it is required to print the elements common at a specific index in a list of strings, a ‘min’ method, list comprehension and a Boolean flag value can be used.ExampleBelow is a demonstration of the samemy_list = ["week", "seek", "beek", "reek", 'meek', 'peek'] print("The list is :") print(my_list) ... Read More

Python Program to print element with maximum vowels from a List

AmitDiwan

AmitDiwan

Updated on 16-Sep-2021 08:42:08

632 Views

When it is required to print element with maximum vowels from a list, a list comprehension is used.ExampleBelow is a demonstration of the samemy_list = ["this", "week", "is", "going", "great"] print("The list is :") print(my_list) my_result = "" max_length = 0 for element in my_list: ... Read More

Python - Round number of places after the decimal for column values in a Pandas DataFrame

AmitDiwan

AmitDiwan

Updated on 16-Sep-2021 08:41:36

495 Views

To round number of places after the decimal, use the display.precision attribute of the Pandas.At first, import the required Pandas library −import pandas as pdCreate a DataFrame with 2 columns −dataFrame = pd.DataFrame(    { "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], ... Read More

Python program to find the String in a List

AmitDiwan

AmitDiwan

Updated on 16-Sep-2021 08:40:11

522 Views

When it is required to find the string in a list, a simple ‘if’ condition along with ‘in’ operator can be used.ExampleBelow is a demonstration of the samemy_list = [4, 3.0, 'python', 'is', 'fun'] print("The list is :") print(my_list) key = 'fun' print("The key is :") print(key) print("The ... Read More

Advertisements