Add a new value at the beginning of a pandas series

2

How do I add a new value to a pandas series?

I made the copy of the dataframe series and now I need to add a new value to the swap_hoje series, however, instead of being at the end of the series, I need it to be at the beginning, at index 0 .

swap_hoje = swap_dataframe['Valor'].reset_index(drop=True)

I also need to add a value in the string swap_ontem :

swap_ontem = swap_dataframe['Valor'].reset_index(drop=True)

However, in the latter, it will have to be at the end of the series.

    
asked by anonymous 21.06.2017 / 18:51

2 answers

1
  

Edited (Insertion in the middle of the series):
  I made an exception in the middle of the series, see the code.

Try the code below, see it here .

import pandas as pd
serie = pd.Series()

for n in range(5):
    serie = serie.set_value(n, n*n)

# Antes da inserção
print (serie)

Output:

0     0
1     1
2     4
3     9
4    16
dtype: int64

Insertion and append:

# Convert a serie em uma lista
lst = serie.tolist()

# faz a inserção na lista
lst.insert(0,999)

# Inserindo na posição 3
lst.insert(3,'Inserção na posição 3')

# Faz append na lista
lst.append(1000)

# Converte a lista para serie
serie = pd.Series(lst)

# Pos inserção/append
print(serie)

Output:

0                      999
1                        0
2                        1
3    Inserção na posição 3
4                        4
5                        9
6                       16
7                     1000
dtype: object

DEMO

    
21.06.2017 / 19:38
1

Using the series created in this answer :

import pandas as pd
serie = pd.Series()
for n in range(5):
    serie = serie.set_value(n, n*n)

serie
0     0
1     1
2     4
3     9
4    16
dtype: int64

serie2 = pd.Series(100)
serie2
0    100
dtype: int64

serie.append(serie2, ignore_index=True)
0      0
1      1
2      4
3      9
4     16
5    100
dtype: int64

serie2.append(serie, ignore_index=True)
0    100
1      0
2      1
3      4
4      9
5     16
dtype: int64
    
21.06.2017 / 20:34