Create Predefined Function
return f"""
def the_strip_function(input_text:str, prefix:str):
return prefix + " " + "*" + input_text.strip() + "*"
return the_strip_function({ input_text } , { prefix } )
"""
Get the Result of Predefined Function
example_input_text = "Lorem Ipsum"
example_prefix = "-"
<< strip_function ("example_input_text" , "example_prefix" )>>
data = {
'Name' : ['Deden' , 'Emily' , 'Michael' , 'Jessica' , 'Brian' ],
'Age' : [34 , 30 , 35 , 35 , 32 ],
'Country' : ['Indonesia' , 'USA' , 'UK' , 'USA' , 'Australia' ]
}
df = pd .DataFrame (data )
salary = {'Salary' : [5000 , 6000 , 7000 , 5500 , 6500 ]}
df_additional = pd .DataFrame (salary )
df_combined = pd .concat ([df , df_additional ], axis = 1 )
<< pd2org ("df_combined" )>>
Name Age Country Salary
0 Deden 34 Indonesia 5000
1 Emily 30 USA 6000
2 Michael 35 UK 7000
3 Jessica 35 USA 5500
4 Brian 32 Australia 6500
data = {
'Name' : ['Deden' , 'Emily' , 'Michael' , 'Jessica' , 'Brian' ],
'Age' : [34 , 30 , 35 , 35 , 32 ],
'Country' : ['Indonesia' , 'USA' , 'UK' , 'USA' , 'Australia' ]
}
df = pd .DataFrame (data )
salary = {'Salary' : [5000 , 6000 , 7000 , 5500 , 6500 ]}
df_additional = pd .DataFrame (salary )
df_combined = pd .concat ([df , df_additional ], axis = 1 )
<< pd2org ("df_combined" )>>
Print Dataframe to ORG Table
return f"return tabulate({ df } , headers={ df } .columns, tablefmt='orgtbl')"
data = pd .DataFrame ([{"name" : "Deden" , "age" : 34 }, {"name" : "Shinta" , "age" : 33 }])
<< pd2org ("data" )>>
name age
0 Deden 34
1 Shinta 33
Grouping, Aggregating, and Sorting
data = {
'Name' : ['Deden' , 'Emily' , 'Michael' , 'Jessica' , 'Brian' ],
'Age' : [34 , 30 , 35 , 35 , 32 ],
'Country' : ['Indonesia' , 'USA' , 'UK' , 'USA' , 'Australia' ]
}
df = pd .DataFrame (data )
df_grouped = df .groupby ('Country' ).agg ({'Age' : 'mean' , 'Name' : 'count' }).reset_index ()
df_sorted = df_grouped .sort_values (
by = ['Age' ], ascending = False
).rename (columns = {"Name" : "Persons" }).reset_index (drop = True )
#+begin_quote
#+end_quote
<< pd2org ("df_sorted" )>>
Country Age Persons
0 UK 35 1
1 Indonesia 34 1
2 USA 32.5 2
3 Australia 32 1
data = {
'Name' : ['Deden' , 'Emily' , 'Michael' , 'Jessica' , 'Brian' ],
'Age' : [34 , 30 , 35 , 35 , 32 ],
'Country' : ['Indonesia' , 'USA' , 'UK' , 'USA' , 'Australia' ],
'Favourite Food' : [
['Rendang' , 'Nasi Goreng' ],
['Rendang' ],
['Nasi Goreng' ,'Nasi Goreng' ],
['Rendang' , 'Nasi Goreng' ],
['Rendang' , 'Nasi Goreng' ]
]
}
df = pd .DataFrame (data )
def transform_value (x ):
print (type (x ))
if isinstance (x , str ):
return x .upper ()
elif isinstance (x , int ):
return x + 1
elif isinstance (x , list ):
# Sort, remove duplicate
return "," .join (sorted (set (x )))
else :
return x
df_updated = df .applymap (transform_value )
<< pd2org ("df_updated" )>>
Name Age Country Favourite Food
0 DEDEN 35 INDONESIA Nasi Goreng,Rendang
1 EMILY 31 USA Rendang
2 MICHAEL 36 UK Nasi Goreng
3 JESSICA 36 USA Nasi Goreng,Rendang
4 BRIAN 33 AUSTRALIA Nasi Goreng,Rendang
df_missing = pd .DataFrame ({
'Name' : ['Deden' , 'Joy' ,'Emily' , 'Michael' , 'Jessica' , 'Brian' ],
'Age' : [34 , None , 32 , 31 , None , 39 ],
'Country' : ['Indonesia' , 'India' , 'USA' , 'UK' , 'USA' , None ],
})
# Interpolate
df_filled = df_missing .fillna ('N/A' )
<< pd2org ("df_filled" )>>
Name Age Country
0 Deden 34.0 Indonesia
1 Joy N/A India
2 Emily 32.0 USA
3 Michael 31.0 UK
4 Jessica N/A USA
5 Brian 39.0 N/A
Interpolate Missing Value
df_missing = pd .DataFrame ({
'Name' : ['Deden' , 'Joy' ,'Emily' , 'Michael' , 'Jessica' , 'Brian' ],
'Age' : [34 , None , 32 , 31 , None , 39 ],
'Country' : ['Indonesia' , 'India' , 'USA' , 'UK' , 'USA' , None ],
})
# Interpolate
df_interpolated = df_missing .interpolate ()
<< pd2org ("df_interpolated" )>>
Name Age Country
0 Deden 34 Indonesia
1 Joy 33 India
2 Emily 32 USA
3 Michael 31 UK
4 Jessica 35 USA
5 Brian 39
df = pd .DataFrame ({
'Name' : ['Deden' , 'Emily' , 'Michael' , 'Jessica' , 'Brian' ],
'Age' : [34 , 30 , 35 , 35 , 32 ],
'Country' : ['Indonesia' , 'USA' , 'UK' , 'USA' , 'Australia' ],
})