Add item to pandas.Series?
Asked Answered
W

4

25

I want to add an integer to my pandas.Series
Here is my code:

import pandas as pd
input = pd.Series([1,2,3,4,5])
input.append(6)

When i run this, i get the following error:

Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    f.append(6)
  File "C:\Python33\lib\site-packages\pandas\core\series.py", line 2047, in append
    verify_integrity=verify_integrity)
  File "C:\Python33\lib\site-packages\pandas\tools\merge.py", line 878, in concat
    verify_integrity=verify_integrity)
  File "C:\Python33\lib\site-packages\pandas\tools\merge.py", line 954, in __init__
    self.new_axes = self._get_new_axes()
  File "C:\Python33\lib\site-packages\pandas\tools\merge.py", line 1146, in _get_new_axes
    concat_axis = self._get_concat_axis()
  File "C:\Python33\lib\site-packages\pandas\tools\merge.py", line 1163, in _get_concat_axis
    indexes = [x.index for x in self.objs]
  File "C:\Python33\lib\site-packages\pandas\tools\merge.py", line 1163, in <listcomp>
    indexes = [x.index for x in self.objs]
AttributeError: 'int' object has no attribute 'index'

How can I fix that?

Wistrup answered 7/12, 2013 at 13:36 Comment(2)
possible duplicate of How to add a single item to a Pandas SeriesPenitent
None of these questions are mine. Also, your question was asked 12 months ago, but the one I linked to was asked over two years ago.Penitent
G
37

Convert appended item to Series:

>>> ds = pd.Series([1,2,3,4,5]) 
>>> ds.append(pd.Series([6]))
0    1
1    2
2    3
3    4
4    5
0    6
dtype: int64

or use DataFrame:

>>> df = pd.DataFrame(ds)
>>> df.append([6], ignore_index=True)
   0
0  1
1  2
2  3
3  4
4  5
5  6

and last option if your index is without gaps,

>>> ds.set_value(max(ds.index) + 1,  6)
0    1
1    2
2    3
3    4
4    5
5    6
dtype: int64

And you can use numpy as a last resort:

>>> import numpy as np
>>> pd.Series(np.concatenate((ds.values, [6])))
Georgiannageorgianne answered 7/12, 2013 at 13:45 Comment(5)
as of today, .set_value() is deprecated, use .at()/.iat() insteadUnfriendly
As Comte says, set_value deprecated. See Gormoruk's answer for the correct solution. (pandas.pydata.org/pandas-docs/version/0.23.4/generated/…)Finnougric
Note that append is now deprecated and has been removed in pandas 2.0.0, concat could be used as replacement.Leduc
As append is deprecated from pandas 2.0.0, we can use _append. e.g. - ds._append(pd.Series([6]))Twain
"AttributeError: 'Series' object has no attribute append"Reprovable
C
5

Using set_value generates the warning:

FutureWarning: set_value is deprecated and will be removed in a future release. Please use .at[] or .iat[] accessors instead

So you can instead use at like this:

input.at[input.index[-1]+1]=6
Combine answered 28/7, 2018 at 8:43 Comment(0)
D
4

For Pandas >= 2.0:

series.append(s) was Deprecated since version 1.4.0. Removed since version 2.0 (2.0.0 (April 3, 2023))

Recommended approach is pd.concat()

Series + Series:

import pandas as pd
s1 = pd.Series([False, False, False], index=[1,0,2] )
s2 = pd.Series( True )

pd.concat([s1, s2], axis=0, ignore_index=True)
>>>
0    False
1    False
2    False
3     True
dtype: bool

pd.concat([s1, s2], axis=0)
>>>
1    False
0    False
2    False
0     True
dtype: bool

DF + Series:

Adding to a series already in a df, becomes DF + DF: (note the indexing and column rename)

_df = pd.DataFrame( s1, columns=['a'] )
_df
>>>
    a
1   False
0   False
2   False

pd.concat([ _df, 
            s2.to_frame().T.rename({0:'a'},axis=1)
           ], 
           ignore_index=True)
>>>
    a
0   False
1   False
2   False
3   True
Dorathydorca answered 23/11, 2023 at 15:1 Comment(0)
M
-2

here is a one-line answer It is dependent on how the array is defined. If we use Series is a one d array. Use the array notation like x[index] = new value

example

import pandas as pd
input = pd.Series([1,2,3,4,5])
newval = 7 # say
input[len(input)] = newval

or use append if the array is being directly defined.

#if input is defined as []
input2 = [1, 2]
#input2[len(input2)] = 3 # does not work
input2.append(3) #works
input2
Mogul answered 31/5, 2020 at 21:34 Comment(1)
"AttributeError: 'Series' object has no attribute append"Reprovable

© 2022 - 2025 — McMap. All rights reserved.