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
Programming Articles - Page 1054 of 3366
212 Views
When it is required to get ‘K’ length groups with a given summation, an empty list, the ‘product’ method, the ‘sum’ method and the ‘append’ method can be used.ExampleBelow is a demonstration of the samefrom itertools import product my_list = [45, 32, 67, 11, 88, 90, 87, 33, 45, 32] print("The list is : ") print(my_list) N = 77 print("The value of N is ") print(N) K = 2 print("The value of K is ") print(K) my_result = [] for sub in product(my_list, repeat = K): if sum(sub) == N: ... Read More
300 Views
When it is required to split the joined consecutive characters that are similar in nature, the ‘groupby’ method and the ‘join’ method are used.ExampleBelow is a demonstration of the samefrom itertools import groupby my_string = 'pppyyytthhhhhhhoooooonnn' print("The string is :") print(my_string) my_result = ["".join(grp) for elem, grp in groupby(my_string)] print("The result is :") print(my_result)OutputThe original string is : pppyyytthhhhhhhooonnn The resultant split string is : ['ppp', 'yyy', 'tt', 'hhhhhhh', 'ooo', 'nnn']ExplanationThe required packages are imported into the environment.A string is defined and it is displayed on the console.The string is iterated over and it is sorted using ... Read More
2K+ Views
To fetch columns between two DataFrames by Intersection, use the intersection() method. Let us create two DataFrames −# creating dataframe1 dataFrame1 = pd.DataFrame({"Car": ['Bentley', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], }) # creating dataframe2 dataFrame2 = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Units_Sold": [ 100, 110, 150, 80, 200, 90] })Fetch common columns −dataFrame2.columns.intersection(dataFrame1.columns) ExampleFollowing is the complete code −import pandas as pd # creating dataframe1 dataFrame1 = pd.DataFrame({"Car": ['Bentley', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, ... Read More
676 Views
When it is required to determine the index rank of elements in a data structure, a method is defined that takes a list as a parameter. It iteeates over the elements in the list, and performs certain comparisons before changing the values of two variables.ExampleBelow is a demonstration of the samedef find_rank_elem(my_list): my_result = [0 for x in range(len(my_list))] for elem in range(len(my_list)): (r, s) = (1, 1) for j in range(len(my_list)): if ... Read More
147 Views
When it is required to remove non-increasing elements, a simple iteration is used along with comparison of elements.ExampleBelow is a demonstration of the samemy_list = [5, 23, 45, 11, 45, 67, 89, 99, 10, 26, 7, 11] print("The list is :") print(my_list) my_result = [my_list[0]] for elem in my_list: if elem >= my_result[-1]: my_result.append(elem) print("The result is :") print(my_result)OutputThe list is : [5, 23, 45, 11, 45, 67, 89, 99, 10, 26, 7, 11] The result is : [5, 5, 23, 45, 45, 67, 89, 99] ... Read More
832 Views
To append a list to a DataFrame using append(), let us first create a DataFrame. The data is in the form of lists of team rankings for our example − # data in the form of list of team rankings Team = [['India', 1, 100], ['Australia', 2, 85], ['England', 3, 75], ['New Zealand', 4 , 65], ['South Africa', 5, 50]] # Creating a DataFrame and adding columns dataFrame = pd.DataFrame(Team, columns=['Country', 'Rank', 'Points'])Let’s say the following is the row to be append −myList = [["Sri Lanka", 6, 40]] Append the above row in the form of list using append() ... Read More
287 Views
When it is required to get the consecutive ranges of ‘K’ which are greater than ‘N’, the ‘enumerate’ attribute and simple iteration is used.ExampleBelow is a demonstration of the samemy_list = [3, 65, 33, 23, 65, 65, 65, 65, 65, 65, 65, 3, 65] print("The list is :") print(my_list) K = 65 N = 3 print("The value of K is ") print(K) print("The value of N is ") print(N) my_result = [] beg, end = 0, 0 previous = 1 for index, element in enumerate(my_list): if element == K: end = ... Read More
505 Views
To stack a single-level column, use the datafrem.stack(). At first, let us import the required library −import pandas as pdCreate a DataFrame with single-level column −dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35], [40, 45]], index=['w', 'x', 'y', 'z'], columns=['a', 'b'])Stack the DataFrame using the stack() method −dataFrame.stack() ExampleFollowing is the complete code −import pandas as pd # Create DataFrame dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35], [40, 45]], index=['w', 'x', 'y', 'z'], columns=['a', 'b']) # DataFrame print"DataFrame...", dataFrame # stack print"Stacking...", dataFrame.stack()OutputThis will produce the following output −DataFrame... a b w 10 ... Read More
364 Views
When it is required to flatten a nested list into a tuple list, a method is defined that takes a list as a parameter, and uses the ‘isinstance’ method to check if an element belongs to a specific type. Depending on this, the output is displayed.ExampleBelow is a demonstration of the samedef convert_nested_tuple(my_list): for elem in my_list: if isinstance(elem, list): convert_nested_tuple(elem) else: my_result.append(elem) return ... Read More
244 Views
When it is required to create a nested list containing values as the count of list elements, a simple iteration is used.ExampleBelow is a demonstration of the samemy_list = [11, 25, 36, 24] print("The list is :") print(my_list) for element in range(len(my_list)): my_list[element] = [element+1 for j in range(element+1)] print("The resultant list is :") print(my_list)OutputThe list is : [11, 25, 36, 24] The resultant list is : [[1], [2, 2], [3, 3, 3], [4, 4, 4, 4]]ExplanationA list is defined and is displayed on the console.It is iterated over, and it is added to 1 and ... Read More