I need to format a column of pandas object into percentages, currency or strings based on whether it matches a decimal number, is numeric, or neither.
df = pd.DataFrame([[1000, 'Jerry', 'BR1','BR1','Between 0.23% - 0.32%',''],
[1001, 'N/A', 'N/A', 'BR1','0.025',''],
['N/A', 'N/A', 'BR3', 'BR2','0.25',''],
[1003, 'Perry','BR4','BR1','20000','']],
columns=['ID', 'Name', 'Branch', 'Member of','Percent, Comment, or Value','Formatting'],
dtype='object')
Solution so far:
def format_col(df):
if str(df['Percent, Comment, or Value']).replace(".", "", 1).isnumeric():
formatted_val = "{:.0%}". format(float(df['Percent, Comment, or Value']))
if df['Percent, Comment, or Value'].isnumeric():
formatted_val = "${:,.2f}".format(float(df['Percent, Comment, or Value']))
else:
formatted_val = df['Percent, Comment, or Value']
return formatted_val
df['Formatting'] = df.apply(format_col, axis=1)
The function correctly identifies the currency value but not the decimal values:
ID Name Branch Member of Percent, Comment, or Value Formatting
0 1000 Jerry BR1 BR1 Between 0.23% - 0.32% Between 0.23% - 0.32%
1 1001 N/A N/A BR1 0.025 0.025
2 N/A N/A BR3 BR2 0.25 0.25
3 1003 Perry BR4 BR1 20000 $20,000.00
How do I match the decimal numbers?