Python使用pandas將表格數(shù)據(jù)進行處理
前言
任務(wù)描述:
當前有一份excel表格數(shù)據(jù),里面存在缺失值,需要對缺失的數(shù)據(jù)到es數(shù)據(jù)庫中進行查找并對其進行把缺失的數(shù)據(jù)進行補全。
excel表格數(shù)據(jù)如下所示:
一、構(gòu)建es庫中的數(shù)據(jù)
1.1 創(chuàng)建索引
# 創(chuàng)建physical_examination索引 PUT /physical_examination { "settings": { "index": { "number_of_shards": "1", "number_of_replicas": "1" } }, "mappings": { "properties": { "nums": { "type": "integer" }, "name": { "type": "text" }, "sex": { "type": "text" }, "phone": { "type": "integer" }, "result": { "type": "text" } } } }
1.2 插入數(shù)據(jù)
【注意:json數(shù)據(jù)不能格式化換行,否則報錯】
# 向physical_examination索引中添加數(shù)據(jù) POST physical_examination/_bulk {"index":{"_id":"1"}} {"nums":1,"name":"劉一","sex":"男","phone":1234567891,"result":"優(yōu)秀"} {"index":{"_id":"2"}} {"nums":2,"name":"陳二","sex":"男","phone":1234567892,"result":"優(yōu)秀"} {"index":{"_id":"3"}} {"nums":3,"name":"張三","sex":"男","phone":1234567893,"result":"優(yōu)秀"} {"index":{"_id":"4"}} {"nums":4,"name":"李四","sex":"男","phone":1234567894,"result":"優(yōu)秀"} {"index":{"_id":"5"}} {"nums":5,"name":"王五","sex":"男","phone":1234567895,"result":"優(yōu)秀"}
1.3 查詢數(shù)據(jù)
【注意:默認查詢索引下的所有數(shù)據(jù)】
# 查詢索引中的所有數(shù)據(jù) GET physical_examination/_search { "query": { "match_all": {} } }
二、對excel表格中的數(shù)據(jù)處理操作
2.1 導(dǎo)出es查詢的數(shù)據(jù)
- 方法一:直接在kibana或postman查詢的結(jié)果中進行復(fù)制粘貼到一個文檔。
- 方法二:使用kibana導(dǎo)出數(shù)據(jù)。
- 方法三:使用postman導(dǎo)出數(shù)據(jù)保存到本地。
使用python處理數(shù)據(jù),獲取需要的數(shù)據(jù)。
示例代碼:
# 讀取json中體檢信息 with open('./data/physical_examination.json', 'r', encoding='utf-8') as f: data_json = f.read() print(data_json) # 處理json數(shù)據(jù)中的異常數(shù)據(jù) if 'false' in data_json: data_json = data_json.replace('false', "False") data_json = eval(data_json) print(data_json) print(data_json['hits']['hits']) print('*' * 100) valid_data = data_json['hits']['hits'] need_data = [] for data in valid_data: print(data['_source']) need_data.append(data['_source']) print(need_data)
讀取缺失數(shù)據(jù)的excel表格,把缺失的數(shù)據(jù)填補進去。
# 讀取需要填補數(shù)據(jù)的表格 data_xlsx = pd.read_excel('./data/體檢表.xlsx', sheet_name='Sheet1') # print(data_xlsx) # 獲取excel表格的行列 row, col = data_xlsx.shape print(row, col) # 修改表格中的數(shù)據(jù) for i in range(row): bb = data_xlsx.iloc[i] print(bb['姓名'], bb['手機號']) if pd.isnull(bb['手機號']): bb['手機號'] = '666' for cc in need_data: if cc['name'] == bb['姓名']: bb['手機號'] = cc['phone'] data_xlsx.iloc[i, 3] = bb['手機號'] print(bb['姓名'], bb['手機號']) print("-" * 100) print(data_xlsx)
將最終處理好的數(shù)據(jù)保存在新建的文件中。
# 保存數(shù)據(jù)到新文件中 data_xlsx.to_excel('./data/new_data.xlsx', sheet_name='Sheet1', index=False, header=True)
完整代碼如下:
import pandas as pd # 讀取json中體檢信息 with open('./data/physical_examination.json', 'r', encoding='utf-8') as f: data_json = f.read() print(data_json) # 處理json數(shù)據(jù)中的異常數(shù)據(jù) if 'false' in data_json: data_json = data_json.replace('false', "False") data_json = eval(data_json) print(data_json) print(data_json['hits']['hits']) print('*' * 100) valid_data = data_json['hits']['hits'] need_data = [] for data in valid_data: print(data['_source']) need_data.append(data['_source']) print(need_data) # 讀取需要填補數(shù)據(jù)的表格 data_xlsx = pd.read_excel('./data/體檢表.xlsx', sheet_name='Sheet1') # print(data_xlsx) # 獲取excel表格的行列 row, col = data_xlsx.shape print(row, col) # 修改表格中的數(shù)據(jù) for i in range(row): bb = data_xlsx.iloc[i] print(bb['姓名'], bb['手機號']) if pd.isnull(bb['手機號']): bb['手機號'] = '666' for cc in need_data: if cc['name'] == bb['姓名']: bb['手機號'] = cc['phone'] data_xlsx.iloc[i, 3] = bb['手機號'] print(bb['姓名'], bb['手機號']) print("-" * 100) print(data_xlsx) # 保存數(shù)據(jù)到新文件中 data_xlsx.to_excel('./data/new_data.xlsx', sheet_name='Sheet1', index=False, header=True)
運行效果,最終處理好的數(shù)據(jù)如下所示:
到此這篇關(guān)于Python使用pandas將表格數(shù)據(jù)進行處理的文章就介紹到這了,更多相關(guān)pandas表格數(shù)據(jù)處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用keras實現(xiàn)非線性回歸(兩種加激活函數(shù)的方式)
這篇文章主要介紹了使用keras實現(xiàn)非線性回歸(兩種加激活函數(shù)的方式),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07python算法練習(xí)之兔子產(chǎn)子(斐波那切數(shù)列)
這篇文章主要給大家介紹python算法練習(xí)兔子產(chǎn)子,文章先進行問題描述及分析然后設(shè)計算法最后再得出完整程序,需要的朋友可以參考一下 文章得具體內(nèi)容2021-10-10Python opencv相機標定實現(xiàn)原理及步驟詳解
這篇文章主要介紹了Python opencv相機標定實現(xiàn)原理及步驟詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04Jmeter并發(fā)執(zhí)行Python 腳本的完整流程
這篇文章主要介紹了Jmeter并發(fā)執(zhí)行 Python 腳本的問題詳解,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09使用Python實現(xiàn)大學(xué)座位預(yù)約功能
這篇文章主要介紹了如何用Python實現(xiàn)大學(xué)座位預(yù)約,今天這個教程教你如何搶到座位,有座位了還怕聽不到課嗎?感興趣的朋友一起看看吧2022-03-03