Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement SQL query data Operation in Pandas

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

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']

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report