Search code examples
pythondataframeappendconcatenation

How to store loop result into a dataframe?


My data is from 1994-2022. original data view

I have tried to append the loop result into a data frame however, it only manage to append the 1 data.

How do I go about it to store all the data?

def portfolio_allocation (data,breakpoint):
pf = pd.DataFrame()

for i in data.index:
    sort = data.sort_values(by = i , axis = 1)
    sorted_data = sort.loc[i]
    new_data = sorted_data.to_frame()

#  pf_01 = less than 10 percentile
    q10 = np.percentile(sorted_data.values, [10])
    pf_1 = new_data[new_data.values < q10]
    pf_01 = pf.append(pf_1)
return pf_01

The result I want:

                1994
              01
stock           
s_1393  0.000003
s_0462  0.000010
s_0678  0.000010
s_1129  0.000014
s_0962  0.000018
...          ...
s_1065  0.003247
s_0766  0.003253
s_0031  0.003272
s_0451  0.003291
s_1600  0.003297

[200 rows x 1 columns]
            1994
              02
stock           
s_1393  0.000003
s_0462  0.000007
s_0678  0.000011
s_0962  0.000016
s_1129  0.000023
...          ...
s_1343  0.003262
s_1207  0.003276
s_1232  0.003322
s_1870  0.003326
s_0557  0.003347

[200 rows x 1 columns]

However, I only obtain 1994 01.


Solution

  • I can't answer it clearly since I don't have your data and what you are trying to do isn't clear.

    But my suggestion is to change the last line in the loop to pf = pf.append(pf_1)

    and the last line in the function to return pf

    Since you did not use the pf variable you initialized in the beginning.

    EDIT:

    Also, if I can remember correctly, pd.append is deprecated, so you better use pd.concat