Python pandas program to print the first n rows of a Dataframe

In this python pandas program, we will print the first n rows of a Dataframe using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dictionary.
  3. Now convert that dictionary to DataFrame using pd.DataFrame().
  4. Print the first n rows of a Dataframe using df.head(n).
				
					import pandas as pd
dictionary = {'marks1':[34,20,32,30],'marks2':[36,22,10,44]}
df = pd.DataFrame(dictionary)
print(df)
print("First n rows: \n",df.head(2))
				
			

Output :

				
					0   marks1  marks2
0      34      36
1      20      22
2      32      10
3      30      44
First n rows: 
    marks1  marks2
0      34      36
1      20      22
				
			

convert a dictionary into DataFrame

print the last n rows of a DataFrame

Python pandas program to convert a dictionary into DataFrame

In this python pandas program, we will convert a dictionary into DataFrame using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dictionary.
  3. Now convert that dictionary to  DataFrame using pd.DataFrame().
  4. Print the output.
				
					import pandas as pd
dictionary = {'marks1':[34,20,32,30],'marks2':[36,22,10,44]}
df = pd.DataFrame(dictionary)
print(df)
				
			

Output :

				
					0   marks1  marks2
0      34      36
1      20      22
2      32      10
3      30      44
				
			

find the index of the first occurrence of the smallest and largest value of a series

print the first n rows of a Dataframe

Find the index of the smallest and largest value using Pandas

In this python pandas program, we will find the index of the smallest and largest value using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Find the index of the smallest and largest values using df.idxmin() and df.idxmax().
  4. Print the output.
				
					import pandas as pd
df = pd.Series([54,25,38,87,67])
print(df)
print("Index of the first smallest and largest value of the series:")
print(df.idxmin())
print(df.idxmax())
				
			

Output :

				
					0    54
1    25
2    38
3    87
4    67
dtype: int64
Index of the first smallest and largest value of the series:
1
3
				
			

convert a dictionary into DataFrame

Convert a series of date strings to a time-series using Pandas

In this python pandas program, we will convert a series of date strings to a time-series using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series of date strings using pd.Series().
  3. Convert a series of date strings to a time-series using pd.to_datetime(df).
  4. Print the output.
				
					import pandas as pd
df = pd.Series(['2 Feb 2020','5/11/2021','7-8-2022'])
print("Original Series:")
print(df)
print("Converting series of date strings to a timeseries:")
print(pd.to_datetime(df))
				
			

Output :

				
					Original Series:
0    2 Feb 2020
1     5/11/2021
2      7-8-2022
dtype: object
Converting series of date strings to a timeseries:
0   2020-02-02
1   2021-05-11
2   2022-07-08
dtype: datetime64[ns]
				
			

calculate the number of characters in each word in a series

Calculate the number of characters in each word in a series using Pandas

In this python pandas program, we will calculate the number of characters in each word in a series using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Calculate the number of characters in each word in a series using df.map(lambda x: len(x)).
  4. Pass the word of the series to the lambda function using df.map().
  5. Use len() to calculate the number of characters in each word.
  6. Print the output.
				
					import pandas as pf
df = pd.Series(['virat','rohit','pant','shikhar'])
print("Original Series:")
print(df)
result = df.map(lambda x: len(x))
print("Number of characters in each word of series:")
print(result)
				
			

Output :

				
					Original Series:
0      virat
1      rohit
2       pant
3    shikhar
dtype: object
Number of characters in each word of series:
0    5
1    5
2    4
3    7
dtype: int64
				
			

convert the first and last character of each word to upper case in each word of a given series

convert a series of date strings to a time-series

Convert the first and last character to upper case using Pandas

In this python pandas program, we will convert the first and last character to upper case using Pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Convert the first and last character to upper case using df.map(lambda x: x[0].upper() + x[1:-1] + x[-1].upper()).
  4. Pass each word of the series to the lambda function using df.map().
  5. Now using indexing and upper() convert the first and last character to upper case.
  6. Print the output.
				
					import pandas as pd
df = pd.Series(['sqatools','python','data','science'])
print("Original Series:")
print(df)
result = df.map(lambda x: x[0].upper() + x[1:-1] + x[-1].upper())
print("First and last character of each word to upper case:")
print(result)
				
			

Output :

				
					Original Series:
0    sqatools
1      python
2        data
3     science
dtype: object
First and last character of each word to upper case:
0    SqatoolS
1      PythoN
2        DatA
3     SciencE
dtype: object
				
			

extract items at given positions of a series

calculate the number of characters in each word in a series

Extract items at given positions of a series using Pandas

In this python pandas program, we will extract items at given positions of a series using pandas.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Extract items at given positions of a series using df.take(positions) where positions are the index no of values that we want to extract.
  4. Print the output.
				
					import pandas as pd
df = pd.Series([3,0,3,2,2,0,3,3,2])
positions = [1,4,7]
print("Original Series:")
print(df)
result = df.take(positions)
print("Extract items at given positions of the said series:")
print(result)
				
			

Output :

				
					Original Series:
0    3
1    0
2    3
3    2
4    2
5    0
6    3
7    3
8    2
dtype: int64
Extract items at given positions of the said series:
1    0
4    2
7    3
dtype: int64

				
			

calculate the frequency of each value of a series

convert the first and last character of each word to upper case in each word of a given series

Calculate the frequency of each value of a series using Pandas

In this python pandas program, we will calculate the frequency of each value of a series using pandas.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Calculate the frequency of each value of a series using df.value_counts().
  4. Print the output.
				
					import pandas as pd
df = pd.Series([3,0,3,2,2,0,3,3,2])
print(df)
result = df.value_counts()
print(result)
				
			

Output :

				
					0    3
1    0
2    3
3    2
4    2
5    0
6    3
7    3
8    2
dtype: int64
3    4
2    3
0    2
dtype: int64
				
			

calculate the minimum value from a series

extract items at given positions of a series

Calculate the minimum value from a series using Pandas

In this python pandas program, we will calculate the minimum value from a series using pandas.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Find the minimum value of the series using df.min().
  4. Print the output.
				
					import pandas as pd
df = pd.Series([54,38,67,87])
print(df)
print("Minumum value: ",df.min())
				
			

Output :

				
					0    54
1    38
2    67
3    87
dtype: int64
Minumum value:  38
				
			

calculate the maximum value from a series

calculate the frequency of each value of a series

Calculate the maximum value from a series using Pandas

In this python pandas program, we will calculate the maximum value from a series using pandas.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a series using pd.Series().
  3. Find the maximum value of the series using df.max().
  4. Print the output.
				
					import pandas as pd
df = pd.Series([54,38,67,87])
print(df)
print("Maximum value: ",df.max())
				
			

Output :

				
					0    54
1    38
2    67
3    87
dtype: int64
Maximum value:  87
				
			

get the items of a series not present in another series

calculate the minimum value from a series