Found 10476 Articles for Python

Python Program to find the cube of each list element

AmitDiwan
Updated on 15-Sep-2021 11:30:55

1K+ Views

When it is required to find the cube of each list element, a simple iteration and the ‘append’ method are used.ExampleBelow is a demonstration of the samemy_list = [45, 31, 22, 48, 59, 99, 0] print("The list is :") print(my_list) my_result = [] for i in my_list: my_result.append(i*i*i) print("The resultant list is :") print(my_result)OutputThe list is : [45, 31, 22, 48, 59, 99, 0] The resultant list is : [91125, 29791, 10648, 110592, 205379, 970299, 0]ExplanationA list is defined and is displayed on the console.An empty list is defined.The original list is iterated over.Every element ... Read More

Print all words occurring in a sentence exactly K times

AmitDiwan
Updated on 15-Sep-2021 11:25:55

381 Views

When it is required to print all the words occurring in a sentence exactly K times, a method is defined that uses the ‘split’ method, ‘remove’ method and the ‘count’ methods. The method is called by passing the required parameters and output is displayed.ExampleBelow is a demonstration of the samedef key_freq_words(my_string, K):    my_list = list(my_string.split(" "))    for i in my_list:       if my_list.count(i) == K:          print(i)          my_list.remove(i) my_string = "hi there how are you, how are u" K = 2 print("The string is :") print(my_string) print"The repeated ... Read More

Python - Custom space size padding in Strings List

AmitDiwan
Updated on 15-Sep-2021 11:15:59

374 Views

When it is required to customize the space size padding in a list of strings, an empty list, an iteration and the ‘append’ method is used.ExampleBelow is a demonstration of the samemy_list = ["Python", "is", "great"] print("The list is :") print(my_list) lead_size = 3 trail_size = 2 my_result = [] for elem in my_list: my_result.append((lead_size * ' ') + elem + (trail_size * ' ')) print("The result is :") print(my_result)OutputThe list is : ['Python', 'is', 'great'] The result is : [' Python ', ' is ', ' great ']ExplanationA list is defined and ... Read More

Python - First occurrence of one list in another

AmitDiwan
Updated on 15-Sep-2021 11:13:46

364 Views

When it is required to find the first occurrence of one list in another list, the ‘set’ attribute and the ‘next’ method is used.ExampleBelow is a demonstration of the samemy_list_1 = [23, 64, 34, 77, 89, 9, 21] my_list_2 = [64, 10, 18, 11, 0, 21] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_list_2 = set(my_list_2) my_result = next((ele for ele in my_list_1 if ele in my_list_2), None) print("The result is :") print(my_result)OutputThe first list is : [23, 64, 34, 77, 89, 9, 21] The second list is : [64, 10, 18, ... Read More

Python - Merge Pandas DataFrame with Inner Join

AmitDiwan
Updated on 15-Sep-2021 09:55:17

1K+ Views

To merge Pandas DataFrame, use the merge() function. The inner join is implemented on both the DataFrames by setting under the “how” parameter of the merge() function i.e. −how = “inner”At first, let us import the pandas library with an alias −import pandas as pd Create DataFrame1 −dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ) Now, create DataFrame2 −dataFrame2 = pd.DataFrame( { ... Read More

Python - Calculate the variance of a column in a Pandas DataFrame

AmitDiwan
Updated on 15-Sep-2021 09:43:21

1K+ Views

To calculate the variance of column values, use the var() method. At first, import the required Pandas library −import pandas as pdCreate a DataFrame with two columns −dataFrame1 = pd.DataFrame(    { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'],       "Units": [100, 150, 110, 80, 110, 90] } ) Finding Variance of "Units" column values using var() function −print"Variance of Units column from DataFrame1 = ", dataFrame1['Units'].var()In the same way, we have calculated the Variance from the 2nd DataFrame.ExampleFollowing is the complete code −import pandas as pd ... Read More

Python - How to reset index after Groupby pandas?

AmitDiwan
Updated on 15-Sep-2021 09:31:26

9K+ Views

To reset index after group by, at first group according to a column using groupby(). After that, use reset_index().At first, import the required library −import pandas as pdCreate a DataFrame with 2 columns −dataFrame = pd.DataFrame( { "Car": ["Audi", "Lexus", "Audi", "Mercedes", "Audi", "Lexus", "Mercedes", "Lexus", "Mercedes"], "Reg_Price": [1000, 1400, 1100, 900, 1700, 1800, 1300, 1150, 1350] } ) Group according to Car column −resDF = dataFrame.groupby("Car").mean()Now, reset index after grouping −resDF.reset_index() ExampleFollowing is the code − import pandas as ... Read More

Python - Sum only specific rows of a Pandas Dataframe

AmitDiwan
Updated on 15-Sep-2021 09:16:36

5K+ Views

To sum only specific rows, use the loc() method. Mention the beginning and end row index using the : operator. Using loc(), you can also set the columns to be included. We can display the result in a new column.At first, let us create a DataFrame. We have Product records in it, including the Opening and Closing Stock −dataFrame = pd.DataFrame({"Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"], "Opening_Stock": [300, 700, 1200, 1500], "Closing_Stock": [200, 500, 1000, 900]})Sum of some rows i.e. 1st two rows. Column names also mentioned in the loc() i.e. Opening_Stock and Closing_Stock. We are displaying result in a new ... Read More

Python - Calculate the median of column values of a Pandas DataFrame

AmitDiwan
Updated on 15-Sep-2021 09:06:09

680 Views

To calculate the median of column values, use the median() method. At first, import the required Pandas library −import pandas as pdNow, create a DataFrame with two columns −dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )Finding the median of a single column “Units” using median() −print"Median of Units column from DataFrame1 = ", dataFrame1['Units'].median() In the same way, we have calculated the median value from the 2nd DataFrame.ExampleFollowing is the complete code ... Read More

Python Pandas – Find the common rows between two Data Frames

AmitDiwan
Updated on 15-Sep-2021 08:56:53

5K+ Views

To find the common rows between two DataFrames, use the merge() method. Let us first create DataFrame1 with two columns −dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )Create DataFrame2 with two columns −dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 250, 150, 80, 130, 90] } )To find the common ... Read More

Advertisements