Check whether only numeric values are present in a column of a DataFrame

In this python pandas program, we will check whether only numeric values are present in a column of 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. Check whether only the numbers are present in a column of a DataFrame using df[‘Numeric’] = list(map(lambda x: x.isdigit(), df[‘Name’])).
  4. It will create a new column in the dataframe that will contain True if the value in the row contains all the numbers characters or else False.
  5. The lambda function will check whether the value in the column contains all the numbers or not.
  6. map() works as an iterator to return a result after applying a function to every item of an iterable 
  7. list() will create a list of all results after applying the lambda function to each value.
  8. Print the output.
				
					import pandas as pd
d = {'Marks':['Pass','88','First Class','90','Distinction']}
df = pd.DataFrame(d)
df['Numeric'] = list(map(lambda x: x.isdigit(), df['Marks']))
print(df)
				
			

Output :

				
					0         Marks  Numeric
0         Pass    False
1           88     True
2  First Class    False
3           90     True
4  Distinction    False
				
			

check whether only the lower case is present in a given column of a DataFrame

check whether only alphabetic values present in a column of a DataFrame

Check whether only the lower case is present in a given column of a DataFrame

In this python pandas program, we will check whether only the lower case is present in a given column of 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. Check whether only the lowercase is present in a column of a DataFrame using df[‘Lowerercase’] = list(map(lambda x: x.islower(), df[‘Name’])).
  4. It will create a new column in the dataframe that will contain True if the value in the name row contains all the lowercase characters or else False.
  5. The lambda function will check whether the value in the name column contains all the lowercase characters or not
  6. list() will create a list of all results after applying the lambda function to each value.
  7. Print the output.
				
					import pandas as pd
d = {'Name':['kate','jason','ROBERT','MARK','dwyane']}
df = pd.DataFrame(d)
df['Lowerrcase'] = list(map(lambda x: x.islower(), df['Name']))
print(df)
				
			

Output :

				
					0     Name  Lowerrcase
0    kate        True
1   jason        True
2  ROBERT       False
3    MARK       False
4  dwyane        True
				
			

check whether only the upper case is present in a column of a DataFrame

check whether only numeric values are present in a column of a DataFrame

Check whether only the upper case is present in a column of a DataFrame

In this python pandas program, we will check whether only the upper case is present in a column of 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. Check whether only the uppercase is present in a column of a DataFrame using df[‘Uppercase’] = list(map(lambda x: x.isupper(), df[‘Name’])).
  4. It will create a new column in the dataframe that will contain True if the value in the name row contains all the uppercase characters or else False.
  5. The lambda function will check whether the value in the name column contains all the uppercase characters or not
  6. list() will create a list of all results after applying the lambda function to each value.
  7. Print the output.
				
					import pandas as pd
d = {'Name':['Kate','Jason','ROBERT','MARK','Dwyane']}
df = pd.DataFrame(d)
df['Uppercase'] = list(map(lambda x: x.isupper(), df['Name']))
print(df)
				
			

Output :

				
					0     Name  Uppercase
0    Kate      False
1   Jason      False
2  ROBERT       True
3    MARK       True
4  Dwyane      False
				
			

convert all the string values in the DataFrame to uppercases

check whether only the lower case is present in a given column of a DataFrame

Convert all the string values in the DataFrame to uppercases

In this python pandas program, we will convert all the string values in the DataFrame to uppercases using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Convert all the string values in the DataFrame to uppercases using df.str.upper().
  4. Print the output.
				
					import pandas as pd
df = pd.Series(['Cricket','Yes','Pass','Fail'])
print(df)
print("Convert all string values of the Series to upper case:")
print(df.str.upper())
				
			

Output :

				
					0    Cricket
1        Yes
2       Pass
3       Fail
dtype: object
Convert all string values of the Series to upper case:
0    CRICKET
1        YES
2       PASS
3       FAIL
dtype: object
				
			

check whether only the upper case is present in a column of a DataFrame

Python Pandas program to select columns by the object datatype from the DataFrame

In this python pandas program, we will select columns by the object datatype from the  DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Select columns by the object datatype from the DataFrame using.select_dtypes(include = “object”).
  4. Print the output.
				
					import pandas as pd
import numpy as np
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33],'Salary':[50000,65000,58000,66000]}
df = pd.DataFrame(d)
print(df)
print("Select string columns")
print(df.select_dtypes(include = "object"))
				
			

Output :

				
					0   Sr.no.   Name  Age  Salary
0       1   Alex   30   50000
1       2   John   27   65000
2       3  Peter   29   58000
3       4  Klaus   33   66000
Select string columns
    Name
0   Alex
1   John
2  Peter
3  Klaus
				
			

reverse the order of columns in a DataFrame

Python Pandas program to reverse the order of columns in a DataFrame

In this python pandas program, we will reverse the order of columns 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. Reverse the order of columns in the dataframe using df.loc[:,::-1].
  4. Print the output.
				
					import pandas as pd
import numpy as np
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33],'Salary':[50000,65000,58000,66000]}
df = pd.DataFrame(d)
print(df)
print("Reverse column order:")
print(df.loc[:,::-1])
				
			

Output :

				
					0   Sr.no.   Name  Age  Salary
0       1   Alex   30   50000
1       2   John   27   65000
2       3  Peter   29   58000
3       4  Klaus   33   66000
Reverse column order:
   Salary  Age   Name  Sr.no.
0   50000   30   Alex       1
1   65000   27   John       2
2   58000   29  Peter       3
3   66000   33  Klaus       4
				
			

reverse the order of rows of a given DataFrame

select columns by the object datatype from the DataFrame

Python Pandas program to reverse the order of rows of a given DataFrame

In this python pandas program,we will reverse the order of rows of a given DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Reverse the order of rows in the dataframe using df.loc[::-1].
  4. Print the output.
				
					import pandas as pd
import numpy as np
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33],'Salary':[50000,65000,58000,66000]}
df = pd.DataFrame(d)
print(df)
print("Reverse row order:")
print(df.loc[::-1])
				
			

Output :

				
					0   Sr.no.   Name  Age  Salary
0       1   Alex   30   50000
1       2   John   27   65000
2       3  Peter   29   58000
3       4  Klaus   33   66000
Reverse row order:
   Sr.no.   Name  Age  Salary
3       4  Klaus   33   66000
2       3  Peter   29   58000
1       2   John   27   65000
0       1   Alex   30   50000
				
			

remove the last n rows of a given DataFrame

reverse the order of columns in a DataFrame

Python Pandas program to remove the last n rows of a given DataFrame

In this python pandas program, we will remove the last n rows of a given DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Create a new dataframe by after removing the first 2 rows using df1 = df.iloc[:2].
  4. Print the output.
				
					import pandas as pd
import numpy as np
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33],'Salary':[50000,65000,58000,66000]}
df = pd.DataFrame(d)
print(df)
print("After removing last 2 rows of the DataFrame:")
df1 = df.iloc[:2]
print(df1)
				
			

Output :

				
					0   Sr.no.   Name  Age  Salary
0       1   Alex   30   50000
1       2   John   27   65000
2       3  Peter   29   58000
3       4  Klaus   33   66000
After removing last 2 rows of the DataFrame:
   Sr.no.  Name  Age  Salary
0       1  Alex   30   50000
1       2  John   27   65000
				
			

remove the first n rows of a DataFrame

reverse the order of rows of a given DataFrame

Python Pandas program to remove the first n rows of a DataFrame

In this python pandas program, w will remove the first n rows of 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. Create a new dataframe by after removing the first 2 rows using df1 = df.iloc[2:].
  4. Print the output.
				
					import pandas as pd
import numpy as np
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33],'Salary':[50000,65000,58000,66000]}
df = pd.DataFrame(d)
print(df)
print("After removing first 2 rows of the DataFrame:")
df1 = df.iloc[2:]
print(df1)
				
			

Output :

				
					0   Sr.no.   Name  Age  Salary
0       1   Alex   30   50000
1       2   John   27   65000
2       3  Peter   29   58000
3       4  Klaus   33   66000
After removing first 2 rows of the DataFrame:
   Sr.no.   Name  Age  Salary
2       3  Peter   29   58000
3       4  Klaus   33   66000
				
			

select all columns except one column in a DataFrame

remove the last n rows of a given DataFrame

Python Pandas program to select all columns except one column in a DataFrame 

In this python pandas program, we will select all columns except one column 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. Create a new dataframe that has all columns except the age column using df.loc[:, df.columns != ‘Age’].
  4. It will select all the rows for all columns except the age column.
  5. Print the output.
				
					import pandas as pd
import numpy as np
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33],'Salary':[50000,65000,58000,66000]}
df = pd.DataFrame(d)
print(df)
print("All columns except 'Age':")
df = df.loc[:, df.columns != 'Age']
print(df)
				
			

Output :

				
					0   Sr.no.   Name  Age  Salary
0       1   Alex   30   50000
1       2   John   27   65000
2       3  Peter   29   58000
3       4  Klaus   33   66000
All columns except 'Age':
   Sr.no.   Name  Salary
0       1   Alex   50000
1       2   John   65000
2       3  Peter   58000
3       4  Klaus   66000
				
			

find the index of a column from the DataFrame

remove the first n rows of a DataFrame