0

well, I am working with a dataframe and I simply want the result that comes out of here to convert it into a new column in a new dataframe. But I do not know how. would you help me? thanks

cont = 12 
for e in df_infl['infl_gen']:
   resultados = (e/df_infl.iloc[cont, 0]) - 1
   resultados = resultados * 100
   cont += 1
if cont >= df_infl.shape[0]:
    print("Ready")
    break
  • 2
    Welcome to stack overflow! Please see [How to make good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and [edit] your question to include a [mcve] with sample input and expected output so that we can better understand how to help you – G. Anderson Nov 17 '20 at 19:43
  • we cant help if we don't know what your data looks like. add `df.to_dict()` output or something – anon01 Nov 17 '20 at 19:48

2 Answers2

1

Just make a list which has all your data and then simply add a column to DataFrame.

cont = 12 
new_column=[0]*12 #Create a list with 12 values as zero (Insertion has to happen from 12th index)
for e in df_infl['infl_gen']:
   resultados = (e/df_infl.iloc[cont, 0]) - 1
   resultados = resultados * 100
   new_column.append(resultados) #Append your result into list
   cont += 1

   if cont >= df_infl.shape[0]:
       print("Ready")
       break

df_infl['new_column_name']=new_column #Add a new column to your current DataFrame
Raghav Gupta
  • 454
  • 3
  • 12
1

If I got your logic right, you can simplify it and make it much faster (because you avoid the loop and can make use of the fast C++ implementations) like this:

df_infl['resultados']= ((df_infl['infl_gen'].shift(12).div(df_infl[name_of_col_0]) - 1) * 100).fillna(0)

For a generated dataframe I get the following output:

    name_col0  infl_gen  new_column_name  resultados
0         127        52         0.000000    0.000000
1         123        68         0.000000    0.000000
2          57       144         0.000000    0.000000
3          57        88         0.000000    0.000000
4         103         4         0.000000    0.000000
5         198       177         0.000000    0.000000
6          79        43         0.000000    0.000000
7         184       183         0.000000    0.000000
8          41        85         0.000000    0.000000
9          15       100         0.000000    0.000000
10         82       105         0.000000    0.000000
11        192       102         0.000000    0.000000
12         74       144       -29.729730  -29.729730
13         53         2        28.301887   28.301887
14         21       132       585.714286  585.714286
15         59        42        49.152542   49.152542
16        161        19       -97.515528  -97.515528
17        132       158        34.090909   34.090909
18        194       110       -77.835052  -77.835052
19        193       171        -5.181347   -5.181347

new_column_name is actually the value calculated by the logic posted by Raghva. So you see both evaluate to the same result.

jottbe
  • 4,228
  • 1
  • 15
  • 31