Python Articles

Page 625 of 852

Python Program to remove a specific digit from every element of the list

AmitDiwan
AmitDiwan
Updated on 16-Sep-2021 588 Views

When it is required to remove specific digit from every element of the list, an iteration and ‘set’ operator and ‘str’ methods are used.ExampleBelow is a demonstration of the samemy_list = [123, 565, 1948, 334, 4598] print("The list is :") print(my_list) key = 3 print("The key is :") print(key) my_result = [] for element in my_list: if list(set(str(element)))[0] == str(key) and len(set(str(element))) == 1: my_result.append('') else: my_result.append(int(''.join([element_1 for element_1 in str(element) if int(element_1) != key]))) print("The result ...

Read More

Python Pandas – Display all the column names in a DataFrame

AmitDiwan
AmitDiwan
Updated on 16-Sep-2021 1K+ Views

To display only the column names in a DataFrame, use dataframe.columns.Import the required library with an alias −import pandas as pd;Following is the DataFrame −dataFrame = pd.DataFrame(    {       "Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Tesla', 'Lexus', 'Mustang'], "Place": ['Delhi', 'Bangalore', 'Hyderabad', 'Chandigarh', 'Pune', 'Mumbai', 'Jaipur'], "Units": [100, 150, 50, 110, 90, 120, 80] } )ExampleDisplay the column names with dataframe.columns. Following is the complete code −import pandas as pd; # create a DataFrame dataFrame = pd.DataFrame(    {       "Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Tesla', 'Lexus', 'Mustang'], "Place": ['Delhi', 'Bangalore', 'Hyderabad', ...

Read More

Python Program to check whether all elements in a string list are numeric

AmitDiwan
AmitDiwan
Updated on 16-Sep-2021 3K+ Views

When it is required to check wether all elements in a list of strings are numeric, the ‘all’ operator is used.ExampleBelow is a demonstration of the samemy_list = ["434", "823", "98", "74", '9870'] print("The list is :") print(my_list) my_result = all(ele.isdigit() for ele in my_list) if(my_result == True): print("All the elements in the list are numeric") else: print("All the elements in the list are not numeric")OutputThe list is : ['434', '823', '98', '74', '9870'] All the elements in the list are numericExplanationA list of integers is defined and is displayed on ...

Read More

Python Program to Extract Rows of a matrix with Even frequency Elements

AmitDiwan
AmitDiwan
Updated on 16-Sep-2021 281 Views

When it is required to extract rows of a matrix with even frequency elements, a list comprehension with ‘all’ operator and ‘Counter’ method are used.ExampleBelow is a demonstration of the samefrom collections import Counter my_list = [[41, 25, 25, 62], [41, 41, 41, 41, 22, 22], [65, 57, 65, 57], [11, 24, 36, 48]] print("The list is :") print(my_list) my_result = [sub for sub in my_list if all( value % 2 == 0 for key, value in list(dict(Counter(sub)).items()))] print("The result is :") print(my_result)OutputThe list is : [[41, 25, 25, 62], [41, 41, 41, 41, 22, 22], ...

Read More

Python Program to convert a list into matrix with size of each row increasing by a number

AmitDiwan
AmitDiwan
Updated on 16-Sep-2021 470 Views

When it is required to convert a list into matrix with the size of every row increasing by a number, the ‘//’ operator and a simple iteration is used.ExampleBelow is a demonstration of the samemy_list = [42, 45, 67, 89, 99, 10, 23, 12, 31, 43, 60, 1, 0] print("The list is :") print(my_list) my_key = 3 print("The value of key is ") print(my_key) my_result = [] for index in range(0, len(my_list) // my_key): my_result.append(my_list[0: (index + 1) * my_key]) print("The resultant matrix is :") print(my_result)OutputThe list is : [42, 45, 67, ...

Read More

Python program to sort tuples by frequency of their absolute difference

AmitDiwan
AmitDiwan
Updated on 16-Sep-2021 356 Views

When it is required to sort tuples by frequency of their absolute difference, the lambda function, the ‘abs’ method and the ‘sorted’ method are used.ExampleBelow is a demonstration of the samemy_list = [(11, 26), (21, 33), (90, 11), (26, 21), (32, 18), (25, 37)] print("The list is :") print(my_list) my_diff_list = [abs(x - y) for x, y in my_list] my_result = sorted(my_list, key = lambda sub: my_diff_list.count(abs(sub[0] - sub[1]))) print("The resultant list is :") print(my_result)OutputThe list is : [(11, 26), (21, 33), (90, 11), (26, 21), (32, 18), (25, 37)] The resultant list is : [(11, ...

Read More

Python Program to Remove First Diagonal Elements from a Square Matrix

AmitDiwan
AmitDiwan
Updated on 16-Sep-2021 252 Views

When it is required to remove the first diagonal elements from a square matrix, the ‘enumerate’ and list comprehension is used.ExampleBelow is a demonstration of the samemy_list = [[45, 67, 85, 42, 11], [78, 99, 10, 13, 0], [91, 23, 23, 64, 23], [91, 11, 22, 14, 35]] print("The list is :") print(my_list) my_result = [] for index, element in enumerate(my_list): my_result.append([ele for index_1, ele in enumerate(element) if index_1 != index]) print("The resultant matrix is :") print(my_result)OutputThe list is : [[45, 67, 85, 42, 11], [78, 99, 10, 13, 0], [91, 23, 23, ...

Read More

Python Program to Extract Strings with at least given number of characters from other list

AmitDiwan
AmitDiwan
Updated on 16-Sep-2021 175 Views

When it is required to extract strings with atleast a given number of characters from the other list, a list comprehension is used.ExampleBelow is a demonstration of the samemy_list = ["Python", "is", "fun", "to", "learn"] print("The list is :") print(my_list) my_char_list = ['e', 't', 's', 'm', 'n'] my_key = 2 print("The value of key is ") print(my_key) my_result = [element for element in my_list if sum(ch in my_char_list for ch in element) >= my_key] print("The resultant list is :") print(my_result)OutputThe list is : ['Python', 'is', 'fun', 'to', 'learn'] The value of key is 2 The ...

Read More

Python program to randomly create N Lists of K size

AmitDiwan
AmitDiwan
Updated on 16-Sep-2021 308 Views

When it is required to create N lists randomly that are K in size, a method is defined that shuffles the values and yields the output.ExampleBelow is a demonstration of the samefrom random import shuffle def gen_random_list(my_val, K): while True: shuffle(my_val) yield my_val[:K] my_list = [12, 45, 76, 32, 45, 88, 99, 0, 1] print("The list is ") print(my_list) K, N = 4, 5 print("The value of K is ") print(K) print("The value of N is ") print(N) my_result = [] ...

Read More

Python - Count distinct in Pandas Aggregation with Numpy

AmitDiwan
AmitDiwan
Updated on 16-Sep-2021 801 Views

To count distinct, use nunique in Pandas. We will groupby a column and find sun as well using Numpy sum().At first, import the required libraries −import pandas as pd import numpy as npCreate a DataFrame with 3 columns. The columns have duplicate values −dataFrame = pd.DataFrame(    {       "Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Lexus'], "Place": ['Delhi', 'Bangalore', 'Delhi', 'Chandigarh', 'Chandigarh'], "Units": [100, 150, 50, 110, 90] } )Count distinct in aggregation agg() with nunique. Calculating the sum for counting, we are using numpy sum() −dataFrame = dataFrame.groupby("Car").agg({"Units": np.sum, "Place": pd.Series.nunique})ExampleFollowing is the code −import ...

Read More
Showing 6241–6250 of 8,519 articles
« Prev 1 623 624 625 626 627 852 Next »
Advertisements