Python Practice › Pandas Programs › Program 75

Number of digits

EasyPandas Programs · 75 of 95

Write a pandas program to count the digits of each integer in a column.

Advertisement
Input
[55000, 75000, 330000, 10000]
Output
    Sales  Sales_length
0   55000             5
1   75000             5
2  330000             6
3   10000             5

Approach

  1. Convert to string and use str.len().

Solution

Python
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
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs