# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
# ---

# # Other Pandas Tricks

import numpy as np
import pandas as pd

# ## Rename Columns

df = pd.DataFrame(np.random.randn(2, 2), columns=["col1", "col2"])
df

df.columns = ["col_one", "col_two"]
df = df.add_prefix("Xx_")
df = df.add_suffix("_xX")
df

df.columns = df.columns.str.replace("Xx", "Oo")
df.columns = df.columns.str.replace("xX", "oO")
df

# ## Reverse Row Order

df.loc[::-1].reset_index(drop=True)

# ## Reverse Column Order

df.loc[:, ::-1]

# ## Split DataFrame into 2 random subsets

df = pd.DataFrame(np.random.rand(100, 100))
df

sub1 = df.sample(frac=0.75, random_state=42)
sub2 = df.drop(sub1.index)

sub1.index = sub1.index.sort_values()
sub2.index = sub2.index.sort_values()
sub1

# ## Filter by Category

df = pd.DataFrame(np.random.rand(6, 2))
df["genre"] = pd.Series(["A", "A", "B", "C", "D", "A"], dtype="category")
df

df[df.genre.isin(["A", "D"])]

df[~df.genre.isin(["A", "D"])]

# ### Filter by Largest Category

# +
print(df.genre.value_counts().nlargest(1))
print(df.genre.value_counts().nlargest(1).index)

df[df.genre.isin(df.genre.value_counts().nlargest(1).index)]
# -

# ## Split String into Multiple Columns

df = pd.DataFrame({
    "name": ["<NAME>" ,"<NAME>"],
    "location": ["Los Angeles, CA", "Washington, DC"]
})
df 

df[["first", "last"]] = df["name"].str.split(' ', expand=True)
df

df["city"] = df["location"].str.split(", ", expand=True)[0]
df

# ## Change Display Options (Not Change Data)
#
# - Visit [Pandas Documentation - Options and settings](https://pandas.pydata.org/pandas-docs/stable/user_guide/options.html) for more options

df = pd.DataFrame(np.random.rand(6, 5))
df

pd.set_option("display.float_format", "${:.2f}".format)
df

pd.reset_option("display.float_format")

# ## Style a DataFrame

df = pd.DataFrame(
    {
        "Date": pd.date_range("20210101", periods=10),
        "Value": np.random.default_rng(42).integers(low=20, high=100, size=10),
        "Area": np.random.default_rng(42).integers(low=0, high=100, size=10),
        "Volume": np.random.default_rng(42).integers(low=1000000, high=10000000, size=10),
    }
)
df

style = {"Date": "{:%Y/%m/%d}", "Value": "${:d}", "Volume": "{:,}"}
df.style.format(style)


df.style.format(style) \
    .hide_index() \
    .highlight_max("Value", color="red") \
    .highlight_min("Value", color="green") \
    .bar("Area", color="orange", align="zero") \
    .background_gradient(subset="Volume", cmap="Greens") \
    .set_caption("Random Chart")


# # Reference
#
# - https://www.youtube.com/watch?v=RlIiVeig3hc

StarcoderdataJupyterScriptsDedupFiltered