Mat*_*sRa 3 python indexing series pandas
我正在通过 Pandas 从 SQL 数据库中选择值,但是当我想向现有 Pandas 系列添加新值时,我收到“无法连接非 NDframe 对象”。所以我不确定我应该如何进行。
sql = "select * from table"
df = pd.read_sql(sql, conn)
datovalue = df['Datovalue']
datovalue.append(35)
Run Code Online (Sandbox Code Playgroud)
这是我打印出来时数据值的样子:
0 736722.0
1 736722.0
2 736723.0
3 736723.0
4 736725.0
Run Code Online (Sandbox Code Playgroud)
如何添加额外的(在本例中为第 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
Run Code Online (Sandbox Code Playgroud)