3

I need some help as a I am a bit lost.

Lets suppose I have a column from a dataframe I need to be filled with certain elements from previous rows. To simplify things i ve made a pd.series:

lista = ['hola','salut','hello','xixie']
index1 = (0, 23,77,88)
lista2 = pd.Series(lista, index = index1)

What I need to do is to fill the gaps between indexes of lista2 with the elements on the list, so I need from row 0 to 22'hola', from 22 to 76 'salut', and so on. The total lenght of the series has to be 88.

I hope you all can understand me well and thanks in advance!

Borja_042
  • 1,071
  • 1
  • 14
  • 26

1 Answers1

3

Try this:

In [55]: lista2.reindex(np.arange(lista2.index.max())).ffill()
Out[55]:
0      hola
1      hola
2      hola
3      hola
4      hola
5      hola
6      hola
7      hola
8      hola
9      hola
10     hola
11     hola
12     hola
13     hola
14     hola
15     hola
16     hola
17     hola
18     hola
19     hola
      ...
68    salut
69    salut
70    salut
71    salut
72    salut
73    salut
74    salut
75    salut
76    salut
77    hello
78    hello
79    hello
80    hello
81    hello
82    hello
83    hello
84    hello
85    hello
86    hello
87    hello
Length: 88, dtype: object
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419