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

AmitDiwan
Published on 20-Sep-2021 12:02:38
To get the maximum of column values, use the max() function. 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] ... Read More

Comparing Timestamp in Python – Pandas

AmitDiwan
Published on 20-Sep-2021 11:48:54
To compare timestamps, we can use index operator i.e. the square brackets. At first, import the required library −import pandas as pdCreate a DataFrame with 3 columns −dataFrame = pd.DataFrame(    {       "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW"],       "Date_of_Purchase": [       ... Read More

How to display Pandas Dataframe in Python without Index?

AmitDiwan
Published on 20-Sep-2021 11:37:38
Use index=False to ignore index. Let us first import the required library −import pandas as pdCreate a DataFrame −dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]], index=['x', 'y', 'z'], columns=['a', 'b'])Select rows by passing label using loc −dataFrame.loc['x'] Display DataFrame without index −dataFrame.to_string(index=False)ExampleFollowing is the code −import pandas as pd ... Read More

Python program to sort Palindrome Words in a Sentence

AmitDiwan
Published on 20-Sep-2021 11:45:10
When it is required to sort the palindrome words present in a sentence, a method is defined that takes a string as a parameter and first ensures that it is a palindrome. Then it sorts all the words of a string and returns it as output.ExampleBelow is a demonstration of ... Read More

Python - Generate all possible permutations of words in a Sentence

AmitDiwan
Published on 20-Sep-2021 11:32:29
When it is required to generate all possible permutations of a word in a sentence, a function is defined. This function iterates over the string and depending on the condition, the output is displayed.ExampleBelow is a demonstration of the samefrom itertools import permutations def calculate_permutations(my_string):    my_list = list(my_string.split()) ... Read More

Python program to extract rows from Matrix that has distinct data types

AmitDiwan
Published on 20-Sep-2021 11:27:14
When it is required to extract rows from a matrix with different data types, it is iterated over and ‘set’ is used to get the distinct types.ExampleBelow is a demonstration of the samemy_list = [[4, 2, 6], ["python", 2, {6: 2}], [3, 1, "fun"], [9, (4, 3)]] print("The list ... Read More

Python - Split list into all possible tuple pairs

AmitDiwan
Published on 20-Sep-2021 11:26:05
When it is required to split the list into all the possible tuple pairs, a method can be defined that takes a list as a parameter and uses list comprehension to iterate through the list and use ‘extend’ methodExampleBelow is a demonstration of the samedef determine_pairings(my_list): if ... Read More

Python program to Reverse a range in list

AmitDiwan
Published on 20-Sep-2021 11:24:18
When it is required to reverse a given range in a list, it is iterated over and the ‘:’ operator along with slicing is used to reverse it.ExampleBelow is a demonstration of the samemy_list = ["Hi", "there", "how", 'are', 'you'] print("The list is : ") print(my_list) m, n ... Read More

Python - Cumulative Mean of Dictionary keys

AmitDiwan
Published on 20-Sep-2021 11:18:43
When it is required to find the cumulative mean of the dictionary keys, an empty dictionary is created, and the original dictionary is iterated over, and the items are accessed. If this is present in the dictionary, the key is appended to the empty dictionary, otherwise the value is placed ... Read More

Python - How to fill NAN values with mean in Pandas?

AmitDiwan
Published on 20-Sep-2021 11:21:47
For mean, use the mean() function. Calculate the mean for the column with NaN and use the fillna() to fill the NaN values with the mean.Let us first import the required libraries −import pandas as pd import numpy as npCreate a DataFrame with 2 columns and some NaN values. We ... Read More
Advertisements