Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append values to Pandas series

I am selecting values from an SQL database through pandas, but when I want to add new values to the existing pandas series, I receive a "cannt concatenate a non-NDframe object". So I am not really sure how I should proceed.

sql = "select * from table"
df = pd.read_sql(sql, conn)
datovalue = df['Datovalue']
datovalue.append(35)

This is is how the datovalues look like when i print it out:

0   736722.0 

1   736722.0 

2  736723.0  

3  736723.0 

4   736725.0

How do add an extra (5th index in this case) value?

like image 592
MathiasRa Avatar asked Mar 07 '23 02:03

MathiasRa


1 Answers

There are several equivalent ways to add data to a series by index:

s = pd.Series([736722.0, 736722.0, 736723.0, 736723.0, 736725.0])

# direct indexing
s[5] = 35

# loc indexing
s.loc[5] = 35

# loc indexing with unknown index
s.loc[s.index.max()+1] = 35

# append with series
s = s.append(pd.Series([35], index=[5]))

# concat with series
s = pd.concat([s, pd.Series([35], index=[5])])

print(s)

0    736722.0
1    736722.0
2    736723.0
3    736723.0
4    736725.0
5        35.0
dtype: float64
like image 105
jpp Avatar answered Mar 14 '23 19:03

jpp