Fill date.frame using for output

Asked

Viewed 62 times

3

I wanted to know how to apply the logic of R to Python. For example: If I enter the code below in R:

x <- data.frame()
for (i in 1:10) {
  x[i,1] <- i
}

It will create in the date.frame x a column, and will fill it with the numbers from 1 to 10, and will end up creating 10 lines I would like to apply the same logic to python

1 answer

4


You can create the data frame this way, passing the range as value and as index:

import pandas as pd
x = pd.DataFrame({'v1':list(range(1,11))}, index = list(range(1,11)))
x

Exit:

   v1
1   1
2   2
3   3
4   4
5   5
6   6
7   7
8   8
9   9
10  10

You can create the list of values and then use:

import pandas as pd
valores = list(range(1,11))
x = pd.DataFrame({'v1': valores}, index = valores)
x

Exit:

   v1
1   1
2   2
3   3
4   4
5   5
6   6
7   7
8   8
9   9
10  10

Browser other questions tagged

You are not signed in. Login or sign up in order to post.