keyword_rows = db[da['product_name'].str.contains('usb')]详细解析
时间: 2023-10-07 21:09:13 浏览: 44
这段代码的作用是从数据库中获取所有产品名称包含"usb"关键词的行数据,并将其赋值给变量"keyword_rows"。
具体解析如下:
1. 首先,使用pandas库中的"str.contains()"方法,判断"dataframe"对象"da"中的"product_name"列中是否包含"usb"关键词,返回一个"Series"对象,值为True或False。
2. 将上一步返回的"Series"对象作为"da"的索引,获取所有符合条件的行数据。
3. 将获取到的行数据赋值给变量"keyword_rows",即得到所有产品名称包含"usb"关键词的行数据。
需要注意的是,这段代码前提是有一个名为"db"的数据库,并且"da"是从该数据库中获取的数据。
相关问题
如下model如何多表join? from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() class Table1(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) class Table2(db.Model): id = db.Column(db.Integer, primary_key=True) table1_id = db.Column(db.Integer) table3_id = db.Column(db.Integer) class Table3(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50))
To join multiple tables, you can use the `join()` method provided by SQLAlchemy. Here's an example of how you can join `Table1`, `Table2` and `Table3` based on their foreign key relationships:
```
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Table1(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
class Table2(db.Model):
id = db.Column(db.Integer, primary_key=True)
table1_id = db.Column(db.Integer, db.ForeignKey('table1.id'))
table3_id = db.Column(db.Integer, db.ForeignKey('table3.id'))
table1 = db.relationship('Table1', backref='table2')
table3 = db.relationship('Table3', backref='table2')
class Table3(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
# Join Table1, Table2 and Table3
result = db.session.query(Table1, Table2, Table3).\
join(Table2, Table1.id == Table2.table1_id).\
join(Table3, Table2.table3_id == Table3.id).all()
```
In this example, we first define the foreign key relationships between `Table1`, `Table2` and `Table3`. Then, we use the `join()` method to join these tables based on their foreign keys. Finally, we execute the query using `all()` to retrieve all the rows from the result set.
Note that you can also use the `filter()` method to filter the result set based on certain conditions.
json_data = db.Column(db.JSON)
这段代码是用来在数据库中定义一个名为json_data的列,该列的数据类型为JSON。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,常用于前后端数据传输和存储。
在使用该列时,可以将一个JSON对象存储到该列中,例如:
```
data = {'name': 'John', 'age': 25}
row = MyTable(json_data=data)
db.session.add(row)
db.session.commit()
```
该代码将一个包含name和age属性的JSON对象存储到MyTable表中的json_data列中。在查询该列时,可以直接使用该列的名称,例如:
```
rows = MyTable.query.all()
for row in rows:
print(row.json_data['name'], row.json_data['age'])
```
该代码将查询MyTable表中的所有行,并输出每行的name和age属性。注意,查询到的json_data属性是一个Python字典,可以像普通字典一样进行操作。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)