Reputation: 385
I want to check if the column data has same number of digit.
I have following dataset
District Prefix Quota
A 98426 783
A 98427 223
A 98446 127
A 98626 51
B 98049 167
B 98079 153
B 98140 120
B 98159 139
B 98169 182
B 98249 86
B 98426 588
B 98446 96
C 98049 104
C 98060 68
C 98149 65
C 98150 68
C 98159 86
C 98160 80
C 98169 113
Code to reproduce:
import pandas as pd
df = pd.DataFrame([
['A', 98426, 783],
['A', 98427, 223],
['A', 98446, 127],
['A', 98626, 51],
['B', 98049, 167],
['B', 98079, 153],
['B', 98140, 120],
['B', 98159, 139],
['B', 98169, 182],
['B', 98249, 86],
['B', 98426, 588],
['B', 98446, 96],
['C', 98049, 104],
['C', 98060, 68],
['C', 98149, 65],
['C', 98150, 68],
['C', 98159, 86],
['C', 98160, 80],
['C', 98169, 113]
],
columns=['District', 'Prefix', 'Quota'])
as you can see in "prefix" column, all the numbers is of 5 digits. but lets suppose there is inconsistency in digits like the first data is of 6 digits or 4 digits. how can I make sure all the digit in integer is same?
I tried len(str(round(df.Prefix.mean())))
that will give the number of digits but it calculates mean first and gives the number of digits of that mean. but I cant check the consistency
Upvotes: 1
Views: 750
Reputation: 82765
This is one approach using str.len
and nunique
.
Ex:
print(df['Prefix'].astype(str).str.len().nunique() == 1) #--> True
Upvotes: 3