Skip to content

Latest commit

 

History

History
244 lines (201 loc) · 6.53 KB

20230516053629-mode_python.org

File metadata and controls

244 lines (201 loc) · 6.53 KB

Mode Python

Predefined Function

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")>>

Pandas

Concat DataFrame

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")>>
NameAgeCountrySalary
0Deden34Indonesia5000
1Emily30USA6000
2Michael35UK7000
3Jessica35USA5500
4Brian32Australia6500

Merge DataFrame

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")>>
nameage
0Deden34
1Shinta33

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")>>
CountryAgePersons
0UK351
1Indonesia341
2USA32.52
3Australia321

Applymap

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")>>
NameAgeCountryFavourite Food
0DEDEN35INDONESIANasi Goreng,Rendang
1EMILY31USARendang
2MICHAEL36UKNasi Goreng
3JESSICA36USANasi Goreng,Rendang
4BRIAN33AUSTRALIANasi Goreng,Rendang

Fill 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_filled = df_missing.fillna('N/A')
<<pd2org("df_filled")>>
NameAgeCountry
0Deden34.0Indonesia
1JoyN/AIndia
2Emily32.0USA
3Michael31.0UK
4JessicaN/AUSA
5Brian39.0N/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")>>
NameAgeCountry
0Deden34Indonesia
1Joy33India
2Emily32USA
3Michael31UK
4Jessica35USA
5Brian39

Group-wise Transform

df = pd.DataFrame({
    'Name': ['Deden', 'Emily', 'Michael', 'Jessica', 'Brian'],
    'Age': [34, 30, 35, 35, 32],
    'Country': ['Indonesia', 'USA', 'UK', 'USA', 'Australia'],
})