My data consists of a lot of Dataframes having the format as below:
raw_data = {
'subject_id': ['1', '2', '3', '4', '5'],
'first_name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'],
'last_name': ['Anderson', 'Ackerman', 'Ali', 'Aoni', 'Atiches'],
'salary': ['2000','200000','3000','300','10000'],
'percentage': [24,434,56,12,245]}
df = pd.DataFrame(raw_data, columns = ['subject_id', 'first_name', 'last_name','salary','percentage'])
0 1 Alex Anderson 2000 24 1 2 Amy Ackerman 200000 434 2 3 Allen Ali 3000 56 3 4 Alice Aoni 300 12 4 5 Ayoung Atiches 10000 245
My goal is to have a custom function for formatting them to the following: `
0 1 Alex Anderson $2,000.00 24.00% 1 2 Amy Ackerman $200,000.00 434.00% 2 3 Allen Ali $3,000.00 56.00% 3 4 Alice Aoni $300.00 12.00% 4 5 Ayoung Atiches $10,000.00 245.00%`
Examples of Dataframes I have:
Description A B C D
School 35 1.01% 0.17% -$139,394
Fishing 5 0.57% 0.21% -$30,572
School c Cur NeT OOS Diff
Scs 663 Med 16-EM $360 $312
Scs_2 720 Pharmacy 16-SOP $360 $312
current :
df['salary'] = df['salary'].apply(lambda x : f"${x:,.2f}")
df['percentage'] = df['percentage'].apply(lambda n : f"{n:.2%}")
I know that lambda functions will do this, but I want to have a custom function def transform: to handle all data types and format every Dataframe that I have. My plan is to use try and catches to handle the datatypes. But need some help with the function.