Found 10476 Articles for Python

Python - Convert one datatype to another in a Pandas DataFrame

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

394 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": [7000.5057, 1500, 5000, 8000, 9000.75768, 6000], "Units": [90, 120, 100, 150, 200, 130] } ) Check the datatypes of the columns created above −dataFrame.dtypesConvert both the types to int32 −dataFrame.astype('int32').dtypes ExampleFollowing is the code −import pandas as pd # ... Read More

Python - Sort rows by Frequency of K

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

167 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 ("The list is ") print(my_list) my_result = [item for items, c in Counter(my_list).most_common() for item in [items] * c] print("The result is ") print(my_result)OutputThe list is [34, 56, 78, 99, 99, 99, 99, 99, 12, 12, 32, 51, 15, 11, 0, 0] The result is [99, 99, 99, ... Read More

Python - Selective consecutive Suffix Join

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

149 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) result = [] temp = [] for element in my_list: temp.append(element) if not element.endswith(suffix): result.append(''.join(temp)) temp = [] print("The result is :") print(result)OutputThe list is : ['Python-', 'fun', 'to-', ... Read More

Python – Center align column headers of a Pandas DataFrame

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'], "Reg_Price": [7000.5057, 1500, 5000.9578, 8000, 9000.75768, 6000] } )Now, center align the column headers −pd.set_option('display.colheader_justify', 'center') ExampleFollowing is the code −import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', ... Read More

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

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

480 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) min_length = min(len(element) for element in my_list) my_result = [] for index in range(0, min_length): flag = True for element in my_list: if element[index] != my_list[0][index]: ... Read More

Python Program to print element with maximum vowels from a List

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

636 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: vowel_length = len([element for element in element if element in ['a', 'e', 'o', 'u', 'i']]) if vowel_length > max_length: max_length = vowel_length my_result = element print("The result is :") print(my_result)OutputThe list is ... Read More

Python program to find the String in a List

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 result is :") if key in my_list: print("The key is present in the list") else: print("The key is not present in the list")OutputThe list is : [4, 3.0, 'python', 'is', 'fun'] The key is : fun The result is : The key is present ... Read More

Python program for most frequent word in Strings List

AmitDiwan
Updated on 16-Sep-2021 08:38:16

5K+ Views

When it is required to find the most frequent word in a list of strings, the list is iterated over and the ‘max’ method is used to get the count of the highest string.ExampleBelow is a demonstration of the samefrom collections import defaultdict my_list = ["python is best for coders", "python is fun", "python is easy to learn"] print("The list is :") print(my_list) my_temp = defaultdict(int) for sub in my_list: for word in sub.split(): my_temp[word] += 1 result = max(my_temp, key=my_temp.get) print("The word that has the ... Read More

Python program to get maximum of each key Dictionary List

AmitDiwan
Updated on 16-Sep-2021 08:35:40

238 Views

When it is required to get the maximum of each key in a list of dictionary elements, a simple iteration is used.ExampleBelow is a demonstration of the samemy_list = [{"Hi": 18, "there": 13, "Will": 89}, {"Hi": 53, "there": 190, "Will": 87}] print("The list is : ") print(my_list) my_result = {} for elem in my_list: for key, val in elem.items(): if key in my_result: my_result[key] = max(my_result[key], val) else: ... Read More

Python program to count the pairs of reverse strings

AmitDiwan
Updated on 16-Sep-2021 08:33:41

172 Views

When it is required to count the pairs of reverse strings, a simple iteration is used.ExampleBelow is a demonstration of the samemy_list = [{"Python": 8, "is": 1, "fun": 9}, {"Python": 2, "is": 9, "fun": 1}, {"Python": 5, "is": 10, "fun": 7}] print("The list is :") print(my_list) result = {} for dic in my_list: for key, value in dic.items(): if key in result: result[key] = max(result[key], value) else: ... Read More

Advertisements