<jupyter_start><jupyter_text>mutate<jupyter_code>from pandas.core.groupby import DataFrameGroupBy
gdf = group_by(df, 'language', "owner")

out = mutate(gdf, rel_stars1 = _.stars - _.stars.min())

ungroup(out)

df.siu_group_by("language", "owner").siu_mutate(rel_stars1 = _.stars - _.stars.min()).siu_ungroup()


ungroup(mutate(out, rel_stars2 = _.stars + _.stars))
df \
  .siu_group_by("language", "owner") \
  .siu_mutate(rel_stars1 = _.stars - _.stars.min()) \
  .siu_ungroup()<jupyter_output><empty_output><jupyter_text>filter<jupyter_code># TODO: change name filter to query?

# regular filter
filter(df, _.stars > 3000, _.stars < 15000)

# grouped filter
gdf = group_by(df, "language")

ungroup(filter(gdf, _.stars != _.stars.min()))<jupyter_output><empty_output><jupyter_text>summarize<jupyter_code># summarize DataFrame
summarize(df, min_stars = _.stars.min())

# summarize grouped DataFrame
gdf = group_by(df, "language")

summarize(gdf, ttl_stars = _.stars.sum(), wat = _.stars.min())<jupyter_output><empty_output><jupyter_text>transmute<jupyter_code>transmute(df, "repo", rel_stars1 = _.stars - _.stars.min())

ungroup(transmute(gdf, "repo", rel_stars1 = _.stars - _.stars.min()))<jupyter_output><empty_output><jupyter_text>select<jupyter_code># thoughts:
#  + can use dynamic values, e.g. colname == .x
#  + if select implements some name class, then nothing magic happening
#    e.g. _.y == _.x is equivalent to lambda cols: cols.y == cols.x
#  - long winded (==, _.y seems harder to read than "y")
# select(df, _.y == _.x, -_.language)

select(df, _.y == _.x, -_.language)
# considered alternative with strings. E.g...
# select(df, "y = x", "language")
# select(df, dict(y = "x"), "language")<jupyter_output><empty_output><jupyter_text>arrange<jupyter_code>arrange(df, -_.owner, _.repo)

arrange(df, _.owner.str.len())<jupyter_output><empty_output><jupyter_text>helpers (if_else, case_when)<jupyter_code>if_else(df.repo == "dplyr", "yeah", "no")
f = if_else(_.repo.str.contains("d"), _.repo, "wat")

f(df)
case_when(df, {
    _.stars > 10000: "incredible!",
    _.stars > 1000: "pretty good!",
    _.stars > 100 : "keep going!",
    True: "I don't know"
})<jupyter_output><empty_output><jupyter_text>nest and unnest<jupyter_code># data column is an array of DataFrames
nest(df, -_.language, key = "data")
unnest(nest(df, -_.language, key = "data"), "data")<jupyter_output><empty_output><jupyter_text>count<jupyter_code>count(df, "language", "owner")
add_count(df, "language", "owner")<jupyter_output><empty_output><jupyter_text>Distinct<jupyter_code>distinct(df, _.language, _keep_all = True)
distinct(df, "language")
distinct(df, lang2 = _.language.str.lower())
gdf = group_by(df, "language")

ungroup(distinct(gdf, lang2 = _.language.str.upper()))<jupyter_output><empty_output><jupyter_text>Joins<jupyter_code>follow = pd.DataFrame({
    'repo': ['pandas', 'dplyr', 'ggplot2', 'plotnine'],
    'repo2': ['pandas', 'dplyr', None, None],
    'follow': [True, False, True, False]
})

join(df, follow, how = "inner", on = "repo")
# join(df, df)

join(df, follow, how = "inner", on = {"repo": "repo2"})

left_join(df, follow, "repo")<jupyter_output><empty_output><jupyter_text>Spread and Gather<jupyter_code>df2 = spread(df, 'language', 'stars')
df2
# this should raise an error, because duplicate id col x key combos
#spread(pd.concat([df, df]), 'language', 'stars')
gather(df2, "key", "value", "R", "python", drop_na = True)

# TODO
# gather(df2, _.key, _.value, _["R":"python"])<jupyter_output><empty_output><jupyter_text>Piping<jupyter_code>f = Pipeable(f = lambda x: x + 1) >> Pipeable(f = lambda x: "x is: {}".format(x))

f(2)
(df
  >> mutate(
       new_repo = _.repo + " waattt",
       case = case_when(_, {_.language == "python": "aw yeah", True: 'wat'})
     )
  >> filter(_.stars > 5000)
  )
df >> group_by(_.language) >> summarize(wat = _.stars.mean())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1851472471190781, 'fraction_numerical': 0.010755441741357234, 'mean_word_length': 5.746113989637306, 'pattern_counts': {'":': 3, '<': 54, '<?xml version=': 0, '>': 68, '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': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '214868', 'license': 'MIT', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1531, 'n_words': 300, 'path': 'examples/examples-dplyr-funcs.ipynb', 'repo_name': 'Alex-Monahan/siuba', 'size': '3905'}