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?
Pandas Series: append() functionThe append() function is used to concatenate two or more Series. Series to append with self. If True, do not use the index labels. If True, raise Exception on creating index with duplicates.
Pandas DataFrame append() MethodThe append() method appends a DataFrame-like object at the end of the current DataFrame. The append() method returns a new DataFrame object, no changes are done with the original DataFrame.
Pandas DataFrame add() Method The add() method adds each value in the DataFrame with a specified value. The specified value must be an object that can be added to the values of the DataFrame.
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])))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With