1

I am writing a python code. In that code, I have to repeat a loop for multiple times. Each time I have to create an object and store some values in it.

In R, I can do that using assign function.

Ex:

for(i in 1:10)
assign(paste0("iter_",i),mtcars[i,])

Now my objective is to develop the same thing in Python. Is it possible in python?

RSK
  • 751
  • 2
  • 7
  • 18
  • 1
    What output are you expecting? N many variables called `iter_` something with... or a list of N vars, or a dictionary of named vars or...? – Jon Clements Aug 09 '18 at 12:13
  • That output should be in data frame. – RSK Aug 09 '18 at 12:14
  • Python itself doesn't have DataFrames... are you using `pandas`? And what does `mtcars[i,]` do - what's your source data etc... – Jon Clements Aug 09 '18 at 12:15
  • Yes, I am using pandas. In R, mtcars is a inbuilt dataset. In the above example, I am storing each record in a separate object.`mtcars[i,]` is i th record in a data frame. Now I am working on pandas DataFrame. – RSK Aug 09 '18 at 12:21
  • There's loads of ways to create a DF from various sources - You might want to look at [pandas.DataFrame](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html) and the related methods at the bottom. – Jon Clements Aug 09 '18 at 12:25
  • Is it possible to store 10 in "a"? `ex:"a"=10` – RSK Aug 09 '18 at 12:50

0 Answers0