I have a Series with different elements length like this: 1, 222, 33, 4, 55555,...
I want to make it the same length of 5, like this: 00001, 00222, 00033, 00004, 55555, ...
How can I do it? Thanks,
You could use zfill, assuming your elements are strings:
import pandas as pd
s = pd.Series(data=['1', '222', '33', '4', '55555'])
result = s.str.zfill(max(map(len, s)))
print(result)
Output
0 00001
1 00222
2 00033
3 00004
4 55555
dtype: object
You can do:
df = pd.DataFrame([1, 222, 44, 55555], columns=['Val'])
max_length = max(df['Val'].astype(str).str.len())
df['SameLen'] = df['Val'].astype(str).map(lambda x: '0' * (max_length - len(x)) + x)
Output:
Val SameLen
0 1 00001
1 222 00222
2 44 00044
3 55555 55555