Python Practice › Pandas Programs › Program 75
Number of digits
Write a pandas program to count the digits of each integer in a column.
Input
[55000, 75000, 330000, 10000]
Output
Sales Sales_length 0 55000 5 1 75000 5 2 330000 6 3 10000 5
Approach
- Convert to string and use
str.len().
Solution
import pandas as pddf = pd.DataFrame({"Sales": [55000, 75000, 330000, 10000]})df["Sales_length"] = df["Sales"].astype(str).str.len()print(df)Output
Sales Sales_length 0 55000 5 1 75000 5 2 330000 6 3 10000 5
Learn the concept
New to this topic? Read the lesson, then come back.