Skip to content

Instantly share code, notes, and snippets.

View bhavaniravi's full-sized avatar
🎯
Focusing

Bhavani Ravi bhavaniravi

🎯
Focusing
View GitHub Profile
@bhavaniravi
bhavaniravi / pandas_playbook.ipynb
Created December 28, 2018 06:42
Pandas - Where to started?
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bhavaniravi
bhavaniravi / pandas-pivot-table-subtotal.py
Created July 11, 2018 11:08
Script to calculate pandas subtotal
table = pd.pivot_table(df, values='Quantity', index =['Country','Region'], columns=["Year","Requester"], aggfunc=[np.sum])
result_df = table
for i in range(2):
df2 = table.groupby(level=i).sum()
if i==0:
tot_sum = df2.sum(axis=i)
df2 = pd.concat([df2,pd.DataFrame([tot_sum]).rename({0:"Grand"})]).sort_index(level=[0])
df2.index = pd.MultiIndex.from_arrays([df2.index.values + '_Total',
len(df2.index) * [''],
len(df2.index) * ['']])
@login_required
def addtoprofile(request, count):
count = int(count)
formsets = (Form1, Form2, Form3)
try:
Form = Form[count]
except IndexError:
return redirect("/")
next_url = "Next"
if count == len(formsets):
@bhavaniravi
bhavaniravi / test_data_generator.py
Last active December 7, 2017 03:51
Creates formset data to replicate data posted by Django forms. This helps to generate data while writing test cases
def instantiate_formset_data(formset_class, data, instances=[], initial=None, action="add"):
"""
Input:
Formset_class
data - Python Dict type form data
Instances - [] if its a CreateForm, or list of instances in case of EditForm
Initial - Initial data if any
Action = define edit/Add action
Output: data --> Formset style Data
@bhavaniravi
bhavaniravi / configure_form_fields.py
Created December 7, 2017 03:46
Django configure form field with bootstrap form-elements
def configure_fields(fields):
"""
Configures form field with bootstrap style
Input forms fields
Output formatted form fields
"""
for field in fields:
if isinstance(fields[field].widget, Textarea):
fields[field].widget.attrs['rows'] = '3'
fields[field].widget.attrs['class'] = 'form-control'