我正在通过 pandas 从 SQL 数据库中选择值,但是当我想向现有的 pandas 系列添加新值时,我收到“无法连接非 NDframe 对象”。所以我不太确定应该如何进行。
sql = "select * from table"
df = pd.read_sql(sql, conn)
datovalue = df['Datovalue']
datovalue.append(35)
这是我打印出来的数据值的样子:
0 736722.0
1 736722.0
2 736723.0
3 736723.0
4 736725.0
如何添加额外的(在本例中为第 5 个索引)值?
最佳答案
有几种等效的方法可以通过索引向系列添加数据:
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
关于python - 将值附加到 Pandas 系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50583868/