Pandas complex calculation based on other columns

2024/11/13 9:39:25

I have successfully created new columns based on arithmetic for other columns but now I have a more challenging need to first select elements based on matches of multiple columns then perform math and having a problem with syntax.

ind_df_qtrly[10:16]
ticker  comp_name   per_fisc_year   per_type    per_fisc_qtr    per_end_date_x  sales   tot_revnu   zacks_x_sector_desc zacks_m_ind_desc    per_end_date_y  mkt_val
562170  AVX AVX CORP    2007    Q   1   2006-06-30  366.408 366.408 Computer and Technology ELECTRONICS 2008-12-31  1353.95
562215  AVX AVX CORP    2007    Q   2   2006-09-30  374.648 374.648 Computer and Technology ELECTRONICS 2008-12-31  1353.95
562260  AVX AVX CORP    2007    Q   3   2006-12-31  378.088 378.088 Computer and Technology ELECTRONICS 2008-12-31  1353.95
562305  AVX AVX CORP    2007    Q   4   2007-03-31  379.351 379.351 Computer and Technology ELECTRONICS 2008-12-31  1353.95
562350  AVX AVX CORP    2008    Q   1   2007-06-30  383.158 383.158 Computer and Technology ELECTRONICS 2008-12-31  1353.95
562395  AVX AVX CORP    2008    Q   2   2007-09-30  400.706 400.706 Computer and Technology ELECTRONICS 2008-12-31  1353.95

I am trying to calculate Trailing 12 Month Value for "tot_revnu" Column starting for Q2 of 2008, which means for every "ticker" I need to create a new Column in the DF which would sum the results of 4 Quarters (2008-Q2, 2008-Q1, 2007-Q4, & 2007-Q3). I have done the same thing to calculate growth using Annual Data but this exercise is proving to be very challenging.

I was able to do this with Annual Data like this

        # Annual Create the Pivot Tableind_table=pd.pivot_table(ind_df_annual, values='tot_revnu',index=['ticker','comp_name','zacks_x_sector_desc','zacks_m_ind_desc','mkt_val'],columns=['per_fisc_year'],aggfunc=np.sum)# Annual calculate Simple and FD Growth and add columns to PivotTableind_table[str(prev_year)+'-Ann Simple']= (ind_table[(prev_year)]  - ind_table[(prev2_year)])/ ind_table[(prev2_year)]ind_table[str(current_year)+'-Ann Simple']= (ind_table[current_year]  - ind_table[prev_year])/ ind_table[prev_year]ind_table[str(current_year)+'-Ann FD']= (ind_table[(str(current_year))+'-Ann Simple']  - ind_table[(str(prev_year))+'-Ann Simple'])/ ind_table[(str(prev_year))+'-Ann Simple']
Answer

You could maybe use a rolling window. Something like this:

ind_df_qtrly['cumulative_tot_revnu'] = ind_df_qtrly['tot_revnu'].rolling(4).sum()

Documentation: pandas.DataFrame.rolling

https://en.xdnf.cn/q/119368.html

Related Q&A

how to generate word from a to z [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

How to webscrape all shoes on nike page using python

I am trying to webscrape all the shoes on https://www.nike.com/w/mens-shoes-nik1zy7ok. How do I scrape all the shoes including the shoes that load as you scroll down the page? The exact information I …

Pyo in Python: name Server not defined

I recently installed Pyo, and I entered Python 3.6 and typedfrom pyo import * s = Server().boot() s.start() sf = SfPlayer("C:\Users\myname\Downloads\wot.mp3", speed=1, loop=True).out()but I …

Limited digits with str.format(), and then only when they matter

If were printing a dollar amount, we usually want to always display two decimal digits.cost1, cost2 = 123.456890123456789, 357.000 print {c1:.2f} {c2:.2f}.format(c1=cost1, c2=cost2)shows123.46 357.00…

How is covariance implemented internally in numpy?

This is the definition of a covariance matrix. http://en.wikipedia.org/wiki/Covariance_matrix#DefinitionEach element in the matrix, except in the principal diagonal, (if I am not wrong) simplifies to E…

Pulling excel rows to display as a grid in tkinter

I am imaging fluorescent cells from a 384-well plate and my software spits out a formatted excel analysis of the data (16 rowsx24 columns of images turns into a list of data, with 2 measurements from e…

Django Migrating DB django.db.utils.ProgrammingError: relation django_site does not exist

Doing a site upgrade for Django, now pushing it to the server when I try python manage.py makemigrations I get this error (kpsga) sammy@kpsga:~/webapps/kpsga$ python manage.py makemigrations Traceback …

list intersection algorithm implementation only using python lists (not sets)

Ive been trying to write down a list intersection algorithm in python that takes care of repetitions. Im a newbie to python and programming so forgive me if this sounds inefficient, but I couldnt come …

In keras\tensorflow, How adding CNN layers to last layer of ResNet50V2 that pre-train on imagenet

I am trying to drop the last layer and add a simple CNN instead like the following, model = Sequential() base_model = ResNet50V2(include_top=False, weights="imagenet", input_shape=input_shape…

How to get missing date in columns using python pandas [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…