Add item to pandas.Series?

前端 未结 3 895
我寻月下人不归
我寻月下人不归 2020-12-15 07:57

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)
         


        
3条回答
  •  难免孤独
    2020-12-15 08:43

    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])))
    

提交回复
热议问题