get id from category name

Jessica Calkins

I'm working on my application and i need to get the id for a specific category. for instance if the category is japanese it would give me an id of 2. i'm using sqlalchemy in python here is how i have my connection set up

url = f"postgresql://postgres:postgres@{dbendpoint}:5432/postgres"
engine_db = create_engine(url)

and this is how im setting up the pandas dataframe before sending it to postgres

df = pd.DataFrame()
df['name'] = name
df['address'] = addy
df['categoryID'] = null

where it says null is where i will plan to execute the query to grab the id

Gord Thompson

Assuming that you have the categories in a database table

"category"

category_id  category_name
-----------  -------------
          1  Canadian    
          2  Japanese    
          3  Australian  

you can pull that into a DataFrame and then convert the contents to a dict:

category_df = pd.read_sql_query(
    "SELECT category_id, category_name FROM category", engine
)
category_dict = {
    row.category_name: row.category_id
    for row in category_df.itertuples(index=False)
}
print(category_dict)
# {'Canadian': 1, 'Japanese': 2, 'Australian': 3}

print(category_dict["Japanese"])  # 2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get the category name from category slug in wordpress?

How to get widget data using category id or name in wordpress?

Get Category Name for specified Category id in phtml template - Magento2

What joins in SQL do I need to get the Category name based on the ID from a different table?

using category name vs category id for the value to pass to $_GET

How to get the top name for each category from a Table? Postgresql

in pivot table i stored post_id and category_id how can i get my Post category name from pivot table?

Get the category name from a WooCommerce product using the product id or the category id

Get category name from param ID?

Get custom category name or id on archive.php page Wordpress

opencart - get parent category name/id on a sub-category page? New

get only one row from products table for each category row with category name included

Is there any way to get a WordPress category name from the post title?

Magento get category id by category name and category parent name

How to get the custom category slug and id from custom post type?

get by category id

Get blog category and keywords using blog Id from query

PrestaShop: Get category name from webservice

Get category name from Magento

How to get Category name by id post in wordpress

PHP MySQL Query Get Category Name From Multiple ID Array

how to show category name if the category id is in the parent category column in MySQL?

How to get max id from each category on mysql?

How to get category by name?

How to get name of category based on id?

Could not convert category Name instead of category Id

WooCommerce database query : Get product category image path+name from thumbnail_id

Get the name of the id that matches the category id list in pandas

Woocommerce - Get Parent Category Name for Sub Category