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
- Import pandas library as pd.
- Create a dataframe using pd.DataFrame().
- Create a new column that will contain the length of the integers in the sales column using df[‘Sales’].map(str).apply(len).
- It will convert the integers into strings and then apply the len() function to calculate the length of the integers.
- 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