
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10476 Articles for Python

224 Views
When it is required to filter similar case strings, list comprehension can be used along with ‘isupper’ and ‘islower’ methods.ExampleBelow is a demonstration of the samemy_list = ["Python", "good", "FOr", "few", "CODERS"] print("The list is :") print(my_list) my_result = [sub for sub in my_list if sub.islower() or sub.isupper()] print("The strings with same case are :") print(my_result)OutputThe list is : ['Python', 'good', 'FOr', 'few', 'CODERS'] The strings with same case are : ['good', 'few', 'CODERS']ExplanationA list is defined and is displayed on the console.The list comprehension is used to iterate over the list and check if the strings ... Read More

274 Views
When it is required to find the index value that has been repeated in a list, it is iterated over using the list comprehension and ‘enumerate’.ExampleBelow is a demonstration of the samemy_list = [4, 0, 3, 1] print("The list is :") print(my_list) my_result = [element for sub in ([index] * element for index, element in enumerate(my_list)) for element in sub] print("The result is :") print(my_result)OutputThe list is : [4, 0, 3, 1] The result is : [0, 0, 0, 0, 2, 2, 2, 3]ExplanationA list is defined and is displayed on the console.List comprehension is used to ... Read More

870 Views
To select rows by integer location, use the iloc() function. Mention the index number of the row you want to select.Create a DataFrame −dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]], index=['x', 'y', 'z'], columns=['a', 'b'])Select rows with integer location using iloc() −dataFrame.iloc[1] ExampleFollowing is the code − import pandas as pd # Create DataFrame dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]], index=['x', 'y', 'z'], columns=['a', 'b']) # DataFrame print"DataFrame...", dataFrame # select rows with loc print"Select rows by passing label..." print(dataFrame.loc['z']) # select rows with integer location using iloc print"Select rows by passing integer ... Read More

205 Views
When it is required to convert the suffix denomination to values, the dictionary is iterated over and the ‘replace’ method is used to convert them to values.ExampleBelow is a demonstration of the samemy_list = ["5Cr", "7M", "9B", "12L", "20Tr", "30K"] print("The list is :") print(my_list) value_dict = {"M": 1000000, "B": 1000000000, "Cr": 10000000, "L": 100000, "K": 1000, "Tr": 1000000000000} my_result = [] for element in my_list: for key in value_dict: if key in element: val = ... Read More

4K+ Views
To select rows by passing a label, use the loc() function. Mention the index of which you want to select the row. This is the index label in our example. We have x, y and z as the index label and can be used to select rows with loc().Create a DataFrame −dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]], index=['x', 'y', 'z'], columns=['a', 'b'])Now, select rows with loc. We have passed the index label “z” −dataFrame.loc['z'] ExampleFollowing is the code −import pandas as pd # Create DataFrame dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]], index=['x', 'y', 'z'], columns=['a', ... Read More

507 Views
To cast only a single column, use the astype() method. Let us first create a DataFrame with 2 columns. One of them is a “float64” type and another “int64” −dataFrame = pd.DataFrame( { "Reg_Price": [7000.5057, 1500, 5000, 8000, 9000.75768, 6000], "Units": [90, 120, 100, 150, 200, 130] } )Check the types −dataFrame.dtypes Let’s say we need to cast only a single column “Units” from int64 to int32. For that, use astype() −dataFrame.astype({'Units': 'int32'}).dtypesExampleFollowing is the code − import pandas as pd ... Read More

237 Views
When it is required to check for similar elements in a matrix row, a method is defined that take a matrix as parameter. The map method is used to covert the matrix to a tuple. The matrix values are iterated over and if the frequency is greater than 1, it is displayed on the console.ExampleBelow is a demonstration of the samefrom collections import Counter def find_dupes(my_matrix): my_matrix = map(tuple, my_matrix) freq_dict = Counter(my_matrix) for (row, freq) in freq_dict.items(): if freq>1: print (row) my_matrix = [[1, 1, 0, ... Read More

189 Views
When it is required to check the alternate peak elements in a list, a function is defined that iterates through the list, the adjacent elements of the array are compared and depending on this, the output is displayed on the console.ExampleBelow is a demonstration of the samedef find_peak(my_array, array_length) : if (array_length == 1) : return 0 if (my_array[0] >= my_array[1]) : return 0 if (my_array[array_length - 1] >= my_array[array_length - 2]) : return array_length - 1 for i in range(1, array_length - 1) : ... Read More

168 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 = [row for row in my_list if all(row.count(element) % 2 == 0 for element in row)] print("The result is :") print(my_result)OutputThe list is : [[10, 21, 34, 21, 37], [41, 41, 52, 68, 68, 41], [12, 29], [30, 30, 51, 51]] The result is : [[30, 30, 51, 51]]ExplanationA list ... Read More

284 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 = list(combinations(my_list + replace_list, len(my_list))) print("The result is :") print(my_result)OutputThe list is : [54, 98, 11] The result is : [(54, 98, 11), (54, 98, 8), (54, 98, 10), (54, 11, 8), (54, 11, 10), (54, 8, 10), (98, 11, 8), (98, 11, 10), (98, 8, 10), (11, 8, ... Read More