Home > OS >  Left justify pandas string column with pattern
Left justify pandas string column with pattern

Time:06-21

I have a large pandas dataset with a messy string column wich contains for example:

72.1

61

25.73.20

33.12

I'd like to fill the gaps in order to match a pattern like XX.XX.XX (X are only numbers):

72.10.00

61.00.00

25.73.20

33.12.00

thank you!

CodePudding user response:

How about defining base_string = '00.00.00' then fill other string in each row with base_string:

base_str = '00.00.00'
df = pd.DataFrame({'ms_str':['72.1','61','25.73.20','33.12']})
print(df)

df['ms_str'] = df['ms_str'].apply(lambda x: x base_str[len(x):])
print(df)

Output:

     ms_str
0      72.1
1        61
2  25.73.20
3     33.12


     ms_str
0  72.10.00
1  61.00.00
2  25.73.20
3  33.12.00

CodePudding user response:

Here is a vectorized solution, that works for this particular pattern. First fill with zeros on the right, then replace every third character by a dot:

df['col'].str.ljust(8, fillchar='0').str.replace(r'(..).', r'\1.', regex=True)

Output:

0    72.10.00
1    61.00.00
2    25.73.20
3    33.12.00
Name: col, dtype: object
  • Related