Snippets Collections
import statsmodels.formula.api as smf

model1 = smf.ols('review_score ~ wait_time', data=orders).fit()
print(model1.summary())
## MAP (for Series)
series.map(function) 
Series.map({mapping dict})

#add a column to your df operating on each row
# Let's call this "custom_sum" as "sum" is a built-in function
def custom_sum(row):
    return row.sum()
df['D'] = df.apply(custom_sum, axis=1)

#add as row
df.loc['Row 5'] = df.apply(custom_sum, axis=0)

## APPLY (for DataFrame)
df.apply(lambda col: col.max(), axis = 0)     # default axis
df.apply(lambda row: row[‘A’] + row[‘B’], axis = 1)
df.applymap(my_funct_for_indiv_elements)
    df.applymap(lambda x: '%.2f' % x)

## GROUPBY
group = df.groupby('col_A')
group.mean()
group.apply(np.mean)
group.agg({
    col_A: ['mean', np.sum],
    col_B: my_custom_sum,
    col_B: lambda s: my_custom_sum(s)
    })

group.apply(custom_mean_function)
df.groupby('name')['n'].sum().plot(kind='bar', x='name')
sales.groupby('Platform').get_group('X360')

platform_names = {
    'PS3':'Playstation',
    'PS4':'Playstation',
    'X360':'Xbox',
    'XOne':'Xbox'
}

sales.set_index('Platform').groupby(platform_names).sum()
const refunds = [
  {
    request_amount_to_refund: "3000",
    entity_id: "1850231228424",
    is_zni: false
  },
  {
    request_amount_to_refund: "2000",
    entity_id: "1850231228422",
    is_zni: false
  },
  {
    request_amount_to_refund: "1000",
    entity_id: "1850231228422",
    is_zni: true
  },
  {
    request_amount_to_refund: "500",
    entity_id: "1850231228422",
    is_zni: true
  }
]
    
const refundsFormatted = Object.values(refunds.reduce((acc, curr) => {
  const valueId = `${curr.entity_id}-${curr.is_zni}`

  // Does value already exist ?
  const existingValue = acc[valueId]

  // Values to add
  const existingRequestAmount = parseInt(existingValue?.request_amount_to_refund ?? 0)
  const newRequestAmount = parseInt(curr?.request_amount_to_refund ?? 0)
  const quantity = existingValue?.quantity ? existingValue?.quantity + 1 : 1

  const newValue = {
    ...curr,
    request_amount_to_refund: existingRequestAmount + newRequestAmount,
    quantity: quantity
  }
  return ({
    ...acc,
    [valueId]: newValue,
  })
}, {}))

console.log(refundsFormatted)

/**
Output expected 

[
  {
    request_amount_to_refund: "3000",
    entity_id: "1850231228424",
    is_zni: false,
    quantity: 1
  },
  {
    request_amount_to_refund: "2000",
    entity_id: "1850231228422",
    is_zni: false,
    quantity: 1
  },
  {
    request_amount_to_refund: "1500",
    entity_id: "1850231228422",
    is_zni: true,
    quantity: 2
  }
]
*/
star

Sat Nov 19 2022 19:30:56 GMT+0000 (Coordinated Universal Time)

#pandas #groupby
star

Fri Nov 18 2022 22:15:19 GMT+0000 (Coordinated Universal Time)

#pandas #groupby
star

Thu Nov 03 2022 20:23:12 GMT+0000 (Coordinated Universal Time)

#pandas #groupby
star

Sun Mar 27 2022 22:34:06 GMT+0000 (Coordinated Universal Time) https://www.udemy.com/course/the-ultimate-pandas-bootcamp-advanced-python-data-analysis/learn/lecture/19268906#overview

#python #groupby
star

Wed Feb 23 2022 16:31:20 GMT+0000 (Coordinated Universal Time)

#javascript #array #groupby

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension