Found 10476 Articles for Python

Python program to print elements which are multiples of elements given in a list

AmitDiwan
Updated on 16-Sep-2021 08:31:21

330 Views

When it is required to print the elements which are multiples of elements given in a list, a list comprehension is used.ExampleBelow is a demonstration of the samemy_list = [45, 67, 89, 90, 10, 98, 10, 12, 23] print("The list is :") print(my_list) my_division_list = [6, 4] print("The division list is :") print(my_division_list) my_result = [element for element in my_list if all(element % j == 0 for j in my_division_list)] print("The result is :") print(my_result)OutputThe list is : [45, 67, 89, 90, 10, 98, 10, 12, 23] The division list is : [6, 4] The result ... Read More

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

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

500 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'], "Reg_Price": [7000.5057, 1500, 5000.9578, 8000, 9000.75768, 6000] } ) Use the set_option() method to set precision. We have set precision value 2 −pd.set_option('display.precision', 2)ExampleFollowing is the complete code −import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( ... Read More

Python Program to print strings based on the list of prefix

AmitDiwan
Updated on 16-Sep-2021 08:29:27

406 Views

When it is required to print strings based on the list of prefix elements, a list comprehension, the ‘any’ operator and the ‘startswith’ method are used.ExampleBelow is a demonstration of the samemy_list = ["streek", "greet", "meet", "leeks", "mean"] print("The list is : ") print(my_list) prefix_list = ["st", "ge", "me", "re"] print("The prefix list is : ") print(prefix_list) my_result = [element for element in my_list if any(element.startswith(ele) for ele in prefix_list)] print("The result is :") print(my_result)OutputThe list is : ['streek', 'greet', 'meet', 'leeks', 'mean'] The prefix list is : ['st', 'ge', 'me', 're'] The result is : ... Read More

Python Pandas - Convert Nested Dictionary to Multiindex Dataframe

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

1K+ Views

At first, let us create a Nested Dictionary −dictNested = {'Cricket': {'Boards': ['BCCI', 'CA', 'ECB'], 'Country': ['India', 'Australia', 'England']}, 'Football': {'Boards': ['TFA', 'TCSA', 'GFA'], 'Country': ['England', 'Canada', 'Germany'] }}Now, create an Empty Dictionary −new_dict = {}Now, loop to assign values −for outerKey, innerDict in dictNested.items(): for innerKey, values in innerDict.items(): new_dict[(outerKey, innerKey)] = values Convert to Multi-index DataFrame −pd.DataFrame(new_dict)ExampleFollowing is the code −import pandas as pd # Create Nested dictionary dictNested = {'Cricket': {'Boards': ['BCCI', 'CA', 'ECB'], 'Country': ['India', 'Australia', 'England']}, 'Football': {'Boards': ['TFA', 'TCSA', 'GFA'], ... Read More

Python prorgam to remove duplicate elements index from other list

AmitDiwan
Updated on 16-Sep-2021 08:26:14

264 Views

When it is required to remove duplicate elements index from other list, the ‘enumerate’ attribute, the list comprehension and a simple iteration are used.ExampleBelow is a demonstration of the samemy_list_1 = [4, 5, 6, 5, 4, 7, 8, 6] my_list_2 = [1, 7, 6, 4, 7, 9, 10, 11] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) temp_set = set() temp = [] for index, value in enumerate(my_list_1): if value not in temp_set: temp_set.add(value) else: ... Read More

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

AmitDiwan
Updated on 16-Sep-2021 08:22:09

538 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 Program to check whether all elements in a string list are numeric

AmitDiwan
Updated on 16-Sep-2021 08:19:57

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 replace elements of a list based on the comparison with a number

AmitDiwan
Updated on 16-Sep-2021 08:17:32

445 Views

When it is required to replace elements of a list based on the comparison with a number, a simple iteration is used.ExampleBelow is a demonstration of the samemy_list = [32, 37, 44, 38, 92, 61, 28, 92, 20] print("The list is :") print(my_list) my_key = 32 print("The key is ") print(my_key) low, high = 2, 9 my_result = [] for ele in my_list: if ele > my_key: my_result.append(high) else: my_result.append(low) print("The resultant list is :") print(my_result)OutputThe ... Read More

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

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

227 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 Pandas – Display all the column names in a DataFrame

AmitDiwan
Updated on 16-Sep-2021 08:21:07

977 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

Advertisements