Create a list of tuples from a list having a number and its square in each tuple

In this python tuple program, we will create a list of tuples from a list having a number and its square in each tuple.

Steps to solve the program
  1. Take a list as input.
  2. Use the list comprehension method to create a list of tuples from a list having a number and its square in each tuple.
  3. Use for loop to iterate over elements in the list and use pow() to get the square of the elements.
  4. Perform the above operations inside a list to achieve list comprehension.
  5. Print the output.
				
					list1 = [4,6,3,8]

tup = [(val, pow(val, 2)) for val in list1]
print(tup)
				
			

Output :

				
					[(4, 16), (6, 36), (3, 9), (8, 64)]
				
			

find the minimum value from a tuple.

create a tuple with different datatypes.

Find the minimum value from a tuple

In this python tuple program, we will find the minimum value from a tuple.

Steps to solve the program
  1. Create a tuple.
  2. Find the minimum value from a tuple using min().
  3. Print the output.
				
					tup = (36,5,79,25)
print("Minimum value: ",min(tup))
				
			

Output :

				
					Minimum value:  5
				
			

find the maximum value from a tuple.

create a list of tuples from a list having a number and its square in each tuple.

Find the maximum value from a tuple

In this python tuple program, we will find the maximum value from a tuple.

Steps to solve the program
  1. Create a tuple.
  2. Find the maximum value from a tuple using max().
  3. Print the output.
				
					tup = (41, 15, 69, 55)
print("Maximum value: ",max(tup))
				
			

Output :

				
					Maximum value:  69
				
			
Solution2 :
				
					# take input tuple with multiple values
tup = (41, 15, 69, 101, 55)
# initiate a variable to store max value
max_val = 0
# interate through tuple values using loop
for val in tup:
    # check if val is greater than max_val
    if val > max_val:
        # assign variable value to max_val.
        max_val = val
    else:
        continue

print("maximum value :", max_val)
				
			

Output :

				
					maximum value : 101
				
			

create a tuple with 2 lists of data.

find the minimum value from a tuple.

Program to create a tuple with 2 lists of data

In this python tuple program, we will create a tuple with 2 lists.

Steps to solve the program
  1. Take two lists as input.
  2. Combine elements having the same index no using zip() and convert that list into a tuple using tuple().
  3. Print the output.
				
					list1 = [4, 6, 8]
list2 = [7, 1, 4]
tup =tuple(zip(list1,list2))
print(tup)
				
			

Output :

				
					((4, 7), (6, 1), (8, 4))
				
			

find the maximum value from a tuple.

Python Pandas program to replace NaNs with the median value in a DataFrame

In this python pandas program, we will replace NaNs with the median value using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Now replace the NaN values in the age column with the median value of the same column using df[‘Age’].fillna(df[‘Age’].median(),inplace=True).
  4. Print the output.
				
					import pandas as pd
import numpy as np
df = pd.DataFrame({'Sr.no.':[1,2,3,4],
                   'Name':['Alex','John','Peter','Klaus'],
                   'Age':[30,np.nan,29,22]})
print("Original Dataframe: \n",df)
df['Age'].fillna(df['Age'].median(),inplace=True)
print("After replacing missing values by mean: \n",df)
				
			

Output :

				
					Original Dataframe: 
    Sr.no.   Name   Age
0       1   Alex  30.0
1       2   John   NaN
2       3  Peter  29.0
3       4  Klaus  22.0
After replacing missing values by mean: 
    Sr.no.   Name   Age
0       1   Alex  30.0
1       2   John  29.0
2       3  Peter  29.0
3       4  Klaus  22.0
				
			

replace the missing values with the most frequent values present in each column of a given DataFrame

import a CSV file

Python Pandas program to replace the NaN values with mode value in a DataFrame

In this python pandas program, we will replace the NaN values with mode value in a DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Now replace the NaN values with the mode value in of the same column using df.fillna(method=’pad’).
  4. Print the output.
				
					import pandas as pd
import numpy as np
df = pd.DataFrame({'Sr.no.':[1,2,3,4,5],
                   'Name':['Alex',np.nan,'Peter','Klaus','Stefan'],
                   'Age':[30,np.nan,29,22,22]})
print("Original Dataframe: \n",df)
result = df.fillna(df.mode().iloc[0])
print(result)
				
			

Output :

				
					Original Dataframe: 
    Sr.no.    Name   Age
0       1    Alex  30.0
1       2     NaN   NaN
2       3   Peter  29.0
3       4   Klaus  22.0
4       5  Stefan  22.0
   Sr.no.    Name   Age
0       1    Alex  30.0
1       2    Alex  22.0
2       3   Peter  29.0
3       4   Klaus  22.0
4       5  Stefan  22.0
				
			

replace NaNs with mean in a DataFrame

replace NaNs with the median value in a DataFrame

Python Pandas program to replace NaNs with mean in a DataFrame

In this python pandas program, we will replace NaNs with mean in a DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Replace the NaN values in the age column with the mean age using df[‘Age’].fillna(df[‘Age’].mean(),inplace=True).
  4. Print the output.
				
					import pandas as pd
import numpy as np
df = pd.DataFrame({'Sr.no.':[1,2,3,4],
                   'Name':['Alex','John','Peter','Klaus'],
                   'Age':[30,np.nan,29,22]})
print("Original Dataframe: \n",df)
df['Age'].fillna(df['Age'].mean(),inplace=True)
print("After replacing missing values by mean: \n",df)
				
			

Output :

				
					Original Dataframe: 
    Sr.no.   Name   Age
0       1   Alex  30.0
1       2   John   NaN
2       3  Peter  29.0
3       4  Klaus  22.0
After replacing missing values by mean: 
    Sr.no.   Name   Age
0       1   Alex  30.0
1       2   John  27.0
2       3  Peter  29.0
3       4  Klaus  22.0
				
			

replace NaNs with the value from the next row in a DataFrame

replace the missing values with the most frequent values present in each column of a given DataFrame

Replace NaNs with the value from the next row in a DataFrame

In this python pandas program, we will replace NaNs with the value from the next row in a DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Now replace the NaN values with the value in the next row of the same column using df.fillna(method=’bfill’).
  4. Print the output
				
					import pandas as pd
import numpy as np
df = pd.DataFrame({'Sr.no.':[1,2,3,4],
                   'Name':['Alex',np.nan,'Peter','Klaus'],
                   'Age':[30,np.nan,29,22]})
print("Original Dataframe: \n",df)
print("Fill the rows where all elements are missing with next values:")
result = df.fillna(method='bfill')
print(result)
				
			

Output :

				
					Original Dataframe: 
    Sr.no.   Name   Age
0       1   Alex  30.0
1       2    NaN   NaN
2       3  Peter  29.0
3       4  Klaus  22.0
Fill the rows where all elements are missing with next values:
   Sr.no.   Name   Age
0       1   Alex  30.0
1       2  Peter  29.0
2       3  Peter  29.0
3       4  Klaus  22.0
				
			

replace NaNs with the value from the previous row in a DataFrame

replace NaNs with mean in a DataFrame