<jupyter_start><jupyter_text>Pandas Extra functions> Extra pandas functions at import<jupyter_code># default_exp pdenhanced
# export
import pandas as pd
from typing import Callable<jupyter_output><empty_output><jupyter_text>Value counts<jupyter_code># export
def list_vc(
    df, colname: str, value: str
) -> pd.DataFrame:
    """
    count the values in a column
        that each cell is a list
    """
    return df[colname].list_vc(value)

def col_list_vc(
    col, value: str
) -> pd.DataFrame:
    """
    count the values in a column
        that each cell is a list
    """
    return pd.DataFrame(
        col.apply(lambda x: value in x).value_counts()
    )

pd.DataFrame.vc = lambda self,col:pd.DataFrame(self[col].value_counts())
pd.Series.list_vc = col_list_vc
pd.DataFrame.list_vc = list_vc<jupyter_output><empty_output><jupyter_text>Rename by rule<jupyter_code># export
def default_rename_rule(x: str) -> str:
    return x.replace(" ", "_").replace("-", "_").lower()


def rename_by_rule(
    df,
    rule: Callable = default_rename_rule
) -> pd.DataFrame:
    """
    rename the columns by a rule function
    """
    df = df.rename(
        columns=dict((c, rule(c)) for c in df.columns))
    return df

pd.DataFrame.rename_by_rule = rename_by_rule<jupyter_output><empty_output><jupyter_text>Rearrage Columns<jupyter_code># export
def column_order(df, *col_names) -> pd.DataFrame:
    """
    df = df.column_order("col1", "col2", "col3")
    will put col1, col2, and col3 as the 1st 3 column
    """
    cols = list(df.columns)
    
    for col_name in list(col_names)[::-1]:
        
        # warn if the column exist
        if col_name not in cols:
            print(f"Column:'{col_name}' not in dataframe")
            continue
        cols.insert(0, cols.pop(cols.index(col_name)))
    return df[cols]

pd.DataFrame.column_order = column_order<jupyter_output><empty_output><jupyter_text>Testing<jupyter_code>from sklearn.datasets import california_housing

cdata = california_housing.fetch_california_housing()

df = pd.DataFrame(cdata["data"], columns=cdata["feature_names"])
df["old"] = df.HouseAge>20
df.vc("old")
df.rename_by_rule()
df.column_order("old","AveOccup")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12455035971223022, 'fraction_numerical': 0.00539568345323741, 'mean_word_length': 3.89010989010989, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '204641', 'license': 'MIT', 'n_tokens_mistral': 803, 'n_tokens_neox': 795, 'n_words': 173, 'path': 'nbs/01_pandas_extra.ipynb', 'repo_name': 'raynardj/forgebox', 'size': '2224'}