Replicating items in a pandas list

I need help as I am a little lost.

Suppose I have a column from a dataframe. I need to fill in certain items from the previous lines. To keep things simple, I did pd.series:

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

      

What I need to do is fill in the spaces between the indices of lista2 with the items in the list, so I need line 0 to 22'hola ', 22 to 76' salut ', etc. The total length of the series must be 88.

I hope you all understand me well and thank you in advance!

+3


source to share


1 answer


Try the following:



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

      

+3


source







All Articles