Get the App
SLTechnology News&Howtos  ›  Database  › 

How to implement SQL query data Operation in Pandas

Shulou Source: shulou.com Published: 2022-05-31 19:08:54 10月28日 Update

This article is to share with you about how to achieve SQL query data operation in Pandas, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

1. Select Row

SELECT * FROM

If you want to select the entire table, simply call the name of the table:

# SQL SELECT * FROM table_df # Pandas table_df

SELECT a, b FROM

If you want to select a specific column from the table, list the column you want in double parentheses:

# SQL SELECT column_a, column_b FROM table_df # Pandas table_df [['column_a',' column_b']]

SELECT DISTINCT

Just use .drop\ u duplicates () to get different values:

# SQL SELECT DISTINCT column_a FROM table_df # Pandas table_df ['column_a']. Drop_duplicates ()

SELECT an as b

Use .rename () if you want to rename the column:

# SQL SELECT column_a as Apple, column_b as Banana FROM table_df # Pandas table_df [['column_a',' column_b']] .rename (columns= {'column_a':' Apple', 'column_b':'Banana'})

SELECT CASE WHEN

For the equivalent of "SELECT CASE WHEN", you can use np.select (), where you first specify the selection and value of each option.

# SQL SELECT CASE WHEN column_a > 30 THEN "Large" WHEN column_a 30, table_df ['column_b']

Tags: Just select parameter parenthesis condition sort data query function ascending order multiple more knowledge article different practical same middle finger name size Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker NVidia Shulou Technology Microsoft Linux