Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to store results from for-loop into dataframe columns (Python 3)

I am new to Python and trying to store results from a for-loop into dataframe columns (Python 3). Let’s say I have the following data:

time=[1,2,3,4]
i=[1,2,3,4,5]
j=[10,20,30,40,50]
k=[100,200,300,400,500]
data_ijk=list(zip(i,j,k))

[(1, 10, 100), (2, 20, 200), (3, 30, 300), (4, 40, 400), (5, 50, 500)]

I want to loop through the data_ijk to solve the following equation:

eq=(i+j+k)*t

and put the results of equation into dataframe with column labels
col_labels=["A","B","C","D","E"] for each set of ijk and rows considering time (t).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The expected output should look something like this:

t A B C D E
1 111 222 333 444 555
2 222 444 666 888 1100
3 333 666 999 1332 1665
4 444 888 1332 1776 2220

I know i need to define dataframe

df = pd.DataFrame(columns=[col_labels])

and I assume I should create nested for loop with data_abc and time, but I have no idea how to define to put results in different columns

for i,j,k in data_abc:
  for t in time:
  ...???
   print((i+j+k)*t)

>Solution :

Try:

df = pd.DataFrame(data=[[sum(x)*t for x in zip(i,j,k)] for t in time], 
                  columns=list("ABCDE"), 
                  index=time)

>>> df
     A    B     C     D     E
1  111  222   333   444   555
2  222  444   666   888  1110
3  333  666   999  1332  1665
4  444  888  1332  1776  2220
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading