简体   繁体   中英

How to store results of a for loop to a dataframe given a specific column/row?

I have a dataframe with different signals and returns. I want to do the following:

  1. Subset a specific signal
  2. Calculate the annualized return
  3. Store result to a dataframe

My dataframe looks like this: enter image description here

My code looks like this:

years = range(1990,2019,1)
returns = pd.DataFrame(columns=signals)

for i in signals:
    signal_i = portbase[portbase['signalname'] == i] #Select single signal from dataframe
    
    for j in years:
        signal_i_j = signal_i[signal_i['year'] == j] #Subset single year from signal
        
        return_j = (((signal_i_j['return']/100)+1).prod() -1) * 100 #Calculate annualized return for signal i in year j
        
        returns.loc[j,i] #Add result to dataframe in column i and year j

Everything works except for the last part, where i want so save my results. I want my dataframe to look like this: enter image description here

Signals as columns and Years as rows

Edit: Using the following code works:

df = portbase.groupby(['signalname','year'])['return'].apply(lambda x: (np.prod(1+x/100)-1) * 100).reset_index().T

But my output is still not correct: enter image description here

I tried to convert my output to a dataframe, reset the index and now somehow transpose my signal column as row/header.

Try this code:

df = portbase.groupby(['signalname','year'])['return'].apply(lambda x: (np.prod(1+x/100)-1) * 100).unstack().T

its possible using pivot_table for this.

signal_cols = ['signalname1', 'signalname2']##..
agg_func = lambda x: np.prod(1+x/100)-1)
result = my_df.pivot_table(index='year', columns=signal_cols, values='return', aggfunc=agg_func)

first thing is that it seems you are not using your calculations to save them. j and i are the signals and years.

From the top of my head the.loc() function is for accessing/reading rows and columns by their name. So you're essentially trying to access the data of years and signals of returns.

You might have to put your results in lists and then make a data frame out of them.

I hope my answer has helped somewhat.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM