Found 10476 Articles for Python

Python - Fetch columns between two Pandas DataFrames by Intersection

AmitDiwan
Updated on 21-Sep-2021 08:17:57

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

Python - Index Ranks of Elements

AmitDiwan
Updated on 21-Sep-2021 08:13:43

661 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

Python - Remove non-increasing elements

AmitDiwan
Updated on 21-Sep-2021 08:11:50

137 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

How to append a list to a Pandas DataFrame using append() in Python?

AmitDiwan
Updated on 21-Sep-2021 08:12:00

816 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

Python - Consecutive Ranges of K greater than N

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

276 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

Python – Stacking a single-level column with Pandas stack()?

AmitDiwan
Updated on 21-Sep-2021 08:05:36

485 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

Python program to Flatten Nested List to Tuple List

AmitDiwan
Updated on 21-Sep-2021 08:04:30

352 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

Python - Create nested list containing values as the count of list items

AmitDiwan
Updated on 21-Sep-2021 08:00:49

236 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

Python - How to access the last element in a Pandas series?

AmitDiwan
Updated on 21-Sep-2021 08:01:38

779 Views

We will be using the iat attribute to access the last element, since it is used to access a single value for a row/column pair by integer position.Let us first import the required Pandas library −import pandas as pdCreate a Pandas series with numbers −data = pd.Series([10, 20, 5, 65, 75, 85, 30, 100])Now, get the last element using iat() −data.iat[-1]ExampleFollowing is the code −import pandas as pd # pandas series data = pd.Series([10, 20, 5, 65, 75, 85, 30, 100]) print"Series...", data # get the first element print"The first element in the series = ", data.iat[0] ... Read More

Python - Count the frequency of matrix row length

AmitDiwan
Updated on 21-Sep-2021 07:58:38

221 Views

When it is required to count the frequency of the matrix row length, it is iterated over and its frequency is added to the empty dictionary or incremented if found again.ExampleBelow is a demonstration of the samemy_list = [[42, 24, 11], [67, 18], [20], [54, 10, 25], [45, 99]] print("The list is :") print(my_list) my_result = dict() for element in my_list: if len(element) not in my_result: my_result[len(element)] = 1 else: my_result[len(element)] += 1 print("The result is :") print(my_result)OutputThe ... Read More

Advertisements