Get the length of the integer of a column in a DataFrame

In this python pandas program, we will get the length of the integer of a 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 column that will contain the length of the integers in the sales column using df[‘Sales’].map(str).apply(len).
  4. It will convert the integers into strings and then apply the len() function to calculate the length of the integers.
  5. Print the output.
				
					import pandas as pd
df = pd.DataFrame({'Sales':[55000,75000,330000,10000]})
print("Original DataFrame:")
print(df)
print("Length of sale_amount:")
df['Length'] = df['Sales'].map(str).apply(len)
print(df)
				
			

Output :

				
					Original DataFrame:
    Sales
0   55000
1   75000
2  330000
3   10000
Length of sale_amount:
    Sales  Length
0   55000       5
1   75000       5
2  330000       6
3   10000       5
				
			

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

extract email from a specified column of a given DataFrame

Leave a Comment