AmitDiwan has Published 10744 Articles

Python Pandas - Sort DataFrame in ascending order according to the element frequency

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 12:18:01

851 Views

To sort data in ascending or descending order, use sort_values() method. For ascending order, use the following is the sort_values() method −ascending=TrueImport the required library −import pandas as pd Create a DataFrame with 3 columns −dataFrame = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'BMW', 'Mustang', 'Mercedes', 'Lexus'], ... Read More

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

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 12:02:38

527 Views

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

Python program to sort Palindrome Words in a Sentence

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 11:45:10

464 Views

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

AmitDiwan

Updated on 20-Sep-2021 11:41:10

864 Views

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

How to display Pandas Dataframe in Python without Index?

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 11:37:38

4K+ Views

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 extract rows from Matrix that has distinct data types

AmitDiwan

AmitDiwan

Updated on 20-Sep-2021 11:30:04

184 Views

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

AmitDiwan

Updated on 20-Sep-2021 11:26:05

400 Views

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

AmitDiwan

Updated on 20-Sep-2021 11:24:18

189 Views

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

AmitDiwan

Updated on 20-Sep-2021 11:22:02

296 Views

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

AmitDiwan

Updated on 20-Sep-2021 11:21:47

5K+ Views

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