当前位置:网站首页>Usage of pandas to obtain MySQL data
Usage of pandas to obtain MySQL data
2022-07-03 09:24:00 【zeng31403】
utilize Pandas obtain Mysql data
stay python In the operation mysql, Although convenient , But frequently get data from the server , Efficiency is very low . I tried to use it today pandas from Mysql Extracting data from the database , It's really convenient , And after getting the data once , utilize pandas Rich functions for various analyses , It's also handy .
- First, establish a database connection , Establish a public conn As a connection object
import pymysql
import pandas as pd
class cardb():
conn = None
def connect_db(self):
db_host = "XXXX"
user = "XXXX"
pw = "XXXX"
try:
# Create connection database
self.conn = pymysql.connect(db_host, user, pw, "yiche_car_info", use_unicode=True, charset='utf8mb4')
return self.conn
except Exception as e:
print(" Database connection exception ! error %s", e)
return None
- pandas Use the established connection to obtain data
try:
self.conn.ping(reconnect=True)
except Exception as e:
print("%s" % e)
return None
sql=“select * from viewcarinfo”
df = pd.read_sql_query(sql % (str_info, str_viewname), con=self.conn)
print(df)
Got the data , It's a dataframe Format . A row by row index is called index, Columns are column Like table .
pz_id cartype_id pz_name ... Model level Body type headlamps
0 m139120 m4758 2020 paragraph 1.2L Manual value version ... Light-Duty Vehicle The hatchback Halogen
1 m111122 m2790 2014 paragraph 1.3L The standard version ... Light-Duty Vehicle Three compartment Halogen
2 m133807 m3067 2019 paragraph 1.5L Manual aggressive version ... Light-Duty Vehicle Three compartment LED
3 m133409 m4758 2019 paragraph 1.2LAMT Comfortable edition ... Light-Duty Vehicle The hatchback Halogen
4 m139423 m3167 2020 paragraph 1.4L Manually refresh the new version ... Light-Duty Vehicle Three compartment Halogen
.. ... ... ... ... ... ... ...
343 m129677 m4586 2018 paragraph 5.3L Hand in hand white house one 4 seat ... full-size SUV SUV LED
344 m136613 m3859 2020 paragraph 6.0TW12 The standard version ... Luxury cars Three compartment LED
345 m131852 m4373 2019 paragraph S680 Two tone Collection Edition ... Luxury cars Three compartment matrix LED
346 m132800 m2078 2019 paragraph GT6.0TW12 Open top version ... Luxury cars convertible matrix LED
347 m125538 m3044 2017 paragraph 6.8T Hand integrated long wheelbase version ... Luxury cars Three compartment Xenon
- First use python Establish what needs to be saved json Dictionaries
json_res = {}
json_res["item_name"] = []
json_res["item_option"] = {}
json_res["item_value"] = []
json_res["car_prosys_name"] = car_prosys_name
json_res["car_prosys_value"] = []
json_res["car_prosys_series_value"] = []
json_res["car_pricezone_name"] = car_pricezone_name
json_res["car_pricezone_value"] = []
json_res["car_pricezone_series_value"] = []
- use pandas Analyze the data
Usage of data query :df.loc Is to locate data rows , Add conditions to realize data filtering .
A. Equal filtering or data size filtering
item=" Halogen "
item1 = df.loc[df[" headlamps "] == item]
be equal to ==, Greater than less than <,> You can filter out all data rows that are equal or meet the size condition numerically .
n1 = item1[str_feild].count()
Count all headlights ==“ Halogen ” The number of rows .
B. Text fuzzy search filter
The fuzzy search of text can be used .str.contains This function
Such as
item=" Halogen "
item2 = df.loc[df[" headlamps "].str.contains(item)]
All halogen related data lines can be counted .
n2 = item2[str_feild].count()
C. Union condition lookup
Calculate that all headlights contain “ Halogen ” The number of rows .
You can also combine 2 Search with more than conditions , Such as "&" yes and The effect of , and “|” yes or The effect of .
item3 = df.loc[(df[str_feild] == item) & (df[prosys_name] == ps_name)]
n3 = item2[str_feild].count()
Statistics meet df[str_feild] == item as well as df[prosys_name] == ps_name) Data columns for .
- Export dictionary as json Format , Can be used for flask,Django Data source .
save_path = sys.path[0] + json_path + str_feild_id + ".json"
with open(save_path, 'w') as wr:
json.dump(json_res, wr)
- use pandas Analyze the data
pandas There are many usages at the beginning , Can be said to be numpy Upgraded version ,numpy More for arrays and matrices , and pandas More for
similar excel and sql Advanced data processing , It can also be directly connected with the database , And there are many formats of access and Export ( common
Such as csv,excel,json), It can be said that a big data processing tool .
边栏推荐
- Uc/os self-study from 0
- Digital statistics DP acwing 338 Counting problem
- LeetCode 508. The most frequent subtree elements and
- [point cloud processing paper crazy reading frontier version 8] - pointview gcn: 3D shape classification with multi view point clouds
- LeetCode 535. Encryption and decryption of tinyurl
- IDEA 中使用 Hudi
- [untitled] use of cmake
- Integrated use of interlij idea and sonarqube
- 【点云处理之论文狂读经典版11】—— Mining Point Cloud Local Structures by Kernel Correlation and Graph Pooling
- Move anaconda, pycharm and jupyter notebook to mobile hard disk
猜你喜欢

Pic16f648a-e/ss PIC16 8-bit microcontroller, 7KB (4kx14)
![[point cloud processing paper crazy reading classic version 8] - o-cnn: octree based revolutionary neural networks for 3D shape analysis](/img/fa/36d28b754a9f380bfd86d4562268c3.png)
[point cloud processing paper crazy reading classic version 8] - o-cnn: octree based revolutionary neural networks for 3D shape analysis

【点云处理之论文狂读经典版10】—— PointCNN: Convolution On X-Transformed Points

LeetCode 57. Insert interval
![[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?](/img/fc/5c71e6457b836be04583365edbe08d.png)
[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?

Sword finger offer II 091 Paint the house

AcWing 788. Number of pairs in reverse order

Flink学习笔记(十一)Table API 和 SQL

LeetCode 438. Find all letter ectopic words in the string

LeetCode 535. Encryption and decryption of tinyurl
随机推荐
LeetCode 30. Concatenate substrings of all words
Severity code description the project file line prohibits the display of status error c2440 "initialization": unable to convert from "const char [31]" to "char *"
[graduation season | advanced technology Er] another graduation season, I change my career as soon as I graduate, from animal science to programmer. Programmers have something to say in 10 years
Notes on numerical analysis (II): numerical solution of linear equations
What are the stages of traditional enterprise digital transformation?
[kotlin learning] classes, objects and interfaces - define class inheritance structure
2022-2-14 learning xiangniuke project - Session Management
2022-1-6 Niuke net brush sword finger offer
LeetCode 535. Encryption and decryption of tinyurl
How to check whether the disk is in guid format (GPT) or MBR format? Judge whether UEFI mode starts or legacy mode starts?
【点云处理之论文狂读经典版14】—— Dynamic Graph CNN for Learning on Point Clouds
PowerDesigner does not display table fields, only displays table names and references, which can be modified synchronously
Simple use of MATLAB
LeetCode 1089. Duplicate zero
Basic knowledge of database design
[point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks
On February 14, 2022, learn the imitation Niuke project - develop the registration function
[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis
[point cloud processing paper crazy reading classic version 12] - foldingnet: point cloud auto encoder via deep grid deformation
Win10 quick screenshot