Reputation: 4398
I have a dataframe, df, where I would like to convert all strings from lowercase to uppercase, as well as add leading zeroes for the numerical values that are less than 2 digits.
data
type count
d bu1
d gpa1
d da1
nd da2
nd lapp1
desired
type count
d BU01
d GPA01
d DA01
nd DA02
nd LAPP01
doing
df['count'] = df['count'].str.upper()
I realize how to capitalize the values within the column, but not sure how to add the double digits place. Any suggestion is appreciated.
Upvotes: 0
Views: 87
Reputation: 26676
Another way
s='0'+df['count'].str.extract('(\d+$)')
df['count']=(df['count'].str.replace('\d+$',"")+s[0]).str.upper()
type count
0 d BU01
1 d GPA01
2 d DA01
3 nd DA02
4 nd LAPP01
Upvotes: 1
Reputation: 35626
Try with str.extract
+ str.upper
+ str.zfill
:
s_df = df['count'].str.extract(r'(.*)(\d+)$')
df['count'] = s_df[0].str.upper() + s_df[1].str.zfill(2)
type count
0 d BU01
1 d GPA01
2 d DA01
3 nd DA02
4 nd LAPP01
Explanation:
Extract everything until the ending numbers:
s_df = df['count'].str.extract(r'(.*)(\d+)$')
Separate the values into 2 columns
0 1
0 bu 1
1 gpa 1
2 da 1
3 da 2
4 lapp 1
Then apply upper
to column 0
, and apply zfill
to column 1
.
s_df[0].str.upper()
0 BU
1 GPA
2 DA
3 DA
4 LAPP
Name: 0, dtype: object
s_df[1].str.zfill(2)
0 01
1 01
2 01
3 02
4 01
Name: 1, dtype: object
Then concat together with +
:
s_df[0].str.upper() + s_df[1].str.zfill(2)
0 BU01
1 GPA01
2 DA01
3 DA02
4 LAPP01
dtype: object
DataFrame Used:
df = pd.DataFrame({'type': ['d', 'd', 'd', 'nd', 'nd'],
'count': ['bu1', 'gpa1', 'da1', 'da2', 'lapp1']})
type count
0 d bu1
1 d gpa1
2 d da1
3 nd da2
4 nd lapp1
Upvotes: 1