Skip to main content

Posts

Python Pandas Sorting Dataframe By Columns Which Contains Nan Values (Example)

Sorting dataframe by columns which contains nan values. DataFrame has "sort_values()" method can take an another parameter called "na_position".Using this parameter the rows containing nan values can be pushed to either top or bottom Creating a new dataframe with dictionary  # importing pandas import pandas as pd import numpy as np # animal_data dictionary animal_data = { "Name": ["Cat", "Dog", "Cow"], "Speed": [15, 12, 10], "Sound": ["Meow", "Woof", "Mooo"], "Rank": [1, 5, 3], "Jumping_height": [20, 10, np.NaN], } # creating a dataframe using the animal_data dictionary animal_df = pd.DataFrame(animal_data) # printing animal_df print("animal_df \n", animal_df) animal_df Name Speed Sound Rank Jumping_height 0 Cat 15 Meow 1 20.0 1 Dog 12 Woof 5 10.0 2 Cow 10 Mooo ...

Python Pandas Sorting Dataframe In Ascending or Descending Order Based On Single or Multiple Columns (Example)

Sorting pandas dataframe by single or multiple columns. DataFrame has "sort_values()" method which can be used to sort the dataframe based single or multiple columns , control sorting flow and choose ascending or descending order. Creating a new dataframe with dictionary  # importing pandas import pandas as pd import numpy as np # animal_data dictionary animal_data = { "Name": ["Cat", "Dog", "Cow"], "Speed": [15, 12, 10], "Sound": ["Meow", "Woof", "Mooo"], "Rank": [1, 5, 3], "Jumping_height": [20, 10, np.NaN], } # creating a dataframe using the animal_data dictionary animal_df = pd.DataFrame(animal_data) # printing animal_df print("animal_df \n", animal_df) animal_df Name Speed Sound Rank Jumping_height 0 Cat 15 Meow 1 20.0 1 Dog 12 Woof 5 10.0 2 Cow 10 Mooo 3 ...

Python Pandas Find And Replace String Values With New Values In DataFrame Columns (Example)

Find and replace string values in a column - pandas dataframe String type has str.replace() method which can used for finding and replacing values.We are required to chain this method string type values and pass parameters for value to be searched and new replacing value. Based on the dataset one might be required to run search and replace on columns with mixed datatypes ie. int ,etc. To handle this assert type as string and then chain required string methods Creating a new dataframe with dictionary  # importing pandas import pandas as pd # animal_sp_char_df - with special characters animal_data_with_sp_char = { "Name": ["Cat", "Dog", "Cow", "Tiger", "Goat", "Snake"], "Sound": ["#Meow###", "Wo##of", "Mo#oo", "Rwaar###", "##Baaa", "Skkk##sss"], "Mixed": [123, "#13", "53###", 321, "###456", ...

Python Pandas Select Every Nth Row In DataFrame (Example)

Selecting every nth row from the dataframe. We can select every nth row item from the pandas dataframe by using ".iloc" method. It has the slicing features and stepping features similar to list slicing. iloc is index based and starts from zero Creating a new dataframe with dictionary  # importing pandas import pandas as pd # animal_data_ animal_data_ext = { "Name": ["Cat", "Dog", "Cow","Tiger","Goat","Snake"], "Sound": ["Meow", "Woof", "Mooo","Rwaar","Baaa","Skkksss"], } #creating a dataframe using the animal_data_ext dictionary animal_ext_df = pd.DataFrame(animal_data_ext) #printing animal_ext_df print("animal_ext_df \n", animal_ext_df) animal_ext_df Name Sound 0 Cat Meow 1 Dog Woof 2 Cow Mooo 3 Tiger Rwaar 4 Goat Baaa 5 Snake Skkksss Selecting every nth row (includi...

Topics

Show more