如何在SQLAlchemy中执行原始SQL?
我有一个python web应用程序,它运行在烧瓶上,并通过SQLAlchemy与数据库连接.
我需要一种方法来运行原始SQL.该查询涉及多个表连接以及内联视图.
我试过了:
connection = db.session.connection()
connection.execute( <sql here> )
Run Code Online (Sandbox Code Playgroud)
但我不断收到网关错误.
我有一个python 3函数,定义如下:
def hidden_markov_model(distribution, K=3, N=100, *args):
Run Code Online (Sandbox Code Playgroud)
当我调用该函数时,我收到此错误:
Q_hmm = hidden_markov_model(Gaussian, K=K, N=N,
mu, K*[std**(-2)*np.identity(2)],
)
SyntaxError: positional argument follows keyword argument
Run Code Online (Sandbox Code Playgroud)
怎么了?
我正在尝试实现一个返回图形边缘的方法,由邻接列表/字典表示.
因此,为了遍历字典,首先我遍历键,然后遍历存储在相应键中的每个值.在嵌套的for循环中,我有一个条件,如果一个特定的边,说(a,b)不在边集中,那么将它添加到set - 否则.在我第一次运行时,该方法采用了相同的边 - 也就是说,在边集中,有(a,b)和(b,a).
class Graph():
def __init__(self, grph={}):
self.graph = grph
def get_vertices(self):
for keys in self.graph:
yield keys
def get_edges(self):
edges = set()
for key in self.graph:
for adj_node in self.graph[key]:
if (key, adj_node) not in edges:
edge = (key, adj_node)
edges.add(edge)
else:
pass
return edges
def main():
graph1 = {
'A': ['B','C','D'],
'B': ['A','E'],
'C': ['A', 'D'],
'D': ['A', 'C'],
'E': ['B'],
}
graph_one = Graph(graph1)
print(list(graph_one.get_vertices()))
print(graph_one.get_edges())
if __name__ =='__main__':
main()
Run Code Online (Sandbox Code Playgroud)
输出是:
{( 'A', …
我有一个pandas数据帧,我试图删除所有的对象字段,以便我只剩下数字.
我一直在尝试写一个for循环来完成这项任务,因为我可能需要一遍又一遍地使用不同的数据.
出于某种原因,我无法让它发挥作用.以下是我到目前为止所做的事情
for cols in data:
if data.values.type == object:
numdata = data.drop(axis=1, inplace=True)
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
对于数据中的cols,()1中的AttributeError Traceback(最近一次调用last):----> 2如果data.values.type == object:3 numdata = data.drop(axis = 1,inplace = True)
AttributeError:'numpy.ndarray'对象没有属性'type'
我是一个新手,由于某种原因,我不能得到for循环和if语句逻辑坚持我的脑袋.
为了降低内存成本,我使用以下命令指定了我的 Pandas 数据帧的 dtypes astype()
:
df['A'] = df['A'].astype(int8)
Run Code Online (Sandbox Code Playgroud)
然后我to_csv()
用来存储它,但是当我read_csv()
再次读取它并检查它时dtypes
,我发现它仍然存储在int64
. 如何在将数据保存在本地存储中的同时保留数据类型?
我希望根据日期合并两个熊猫数据帧。问题是第二个数据框不包括第一个数据框的每个日期。我需要使用df1
来自df2
.
+-------------+---------------+-------------+
| DataFrame 1 | | |
+-------------+---------------+-------------+
| Date | Sales loc1 | Sales loc2 |
| 1/1/17 | 100 | 95 |
| 1/2/17 | 125 | 124 |
| 1/3/17 | 115 | 152 |
| ... | | |
| 2/1/17 | 110 | 111 |
+-------------+---------------+-------------+
+-------------+---------+------+
| DataFrame 2 | | |
+-------------+---------+------+
| Date | exp | loc |
| 1/1/17 | 100 | 1 |
| 1/1/17 …
Run Code Online (Sandbox Code Playgroud) 我试图在 Pyspark 的 case 语句中运行子查询,但它抛出异常。如果一个表中的 id 存在于另一个表中,我正在尝试创建一个新标志。
任何人都可以让我知道这在 pyspark 中是否可行?
temp_df=spark.sql("select *, case when key in (select distinct key from Ids) then 1 else 0 end as flag from main_table")
Run Code Online (Sandbox Code Playgroud)
这是错误:
AnalysisException: 'Predicate sub-queries can only be used in a Filter
Run Code Online (Sandbox Code Playgroud) 我有具有object,int64,float64
数据类型的pandas数据框。我想获取列的列名int64 and float64
。我在熊猫中使用以下命令,但似乎不起作用
cat_num_prv_app = [num for num in list(df.columns) if isinstance(num, (np.int64,np.float64))]
Run Code Online (Sandbox Code Playgroud)
以下是我的数据类型
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1670214 entries, 0 to 1670213
Data columns (total 37 columns):
ID 1670214 non-null int64
NAME 1670214 non-null object
ANNUITY 1297979 non-null float64
AMOUNT 1670214 non-null float64
CREDIT 1670213 non-null float64
Run Code Online (Sandbox Code Playgroud)
我想将列名存储ID,ANNUITY,AMOUNT and CREDIT
在变量中,以后可以用它来对数据框进行子集化。
我正在尝试安装tensorflow,但是python 3.7不支持它,所以我想在不使用anaconda的情况下获取python 3.6。
有什么建议吗?
我做了一件蠢事。我使用sudo python3 -m http.server
\n启动了一个本地 python 服务器,我只是要访问一些文档并将其关闭,但我忘记了它并让它运行了 2 天。
该服务器在没有任何私有数据的数字海洋实例上运行。\n但是它确实包含各种编码项目(不受版本控制)、一些我曾经打算转入博客的杂文和其他一些东西。
\n\n我突然想起来并签名了,看起来至少有人试图侵入。我希望这里有人可以帮助我了解发生了什么以及我下一步应该采取什么步骤。这是输出的一部分:
\n\n---------------------------------------- \n5.178.86.78 - - [10/Sep/2019 23:20:49] code 400, message Bad request syntax (\'\\x05\\x01\\x00\') \n5.178.86.78 - - [10/Sep/2019 23:20:49] "" 400 - \n---------------------------------------- \nException happened during processing of request from (\'5.178.86.78\', 30322) \nTraceback (most recent call last): \n File "/usr/lib/python3.6/socketserver.py", line 317, in _handle_request_noblock \n self.process_request(request, client_address) \n File "/usr/lib/python3.6/socketserver.py", line 348, in process_request \n self.finish_request(request, client_address) \n File "/usr/lib/python3.6/socketserver.py", line 361, in finish_request \n self.RequestHandlerClass(request, …
Run Code Online (Sandbox Code Playgroud) python ×10
pandas ×4
dataframe ×1
exploit ×1
flask ×1
pyspark ×1
pyspark-sql ×1
python-3.x ×1
security ×1
sql ×1
sqlalchemy ×1