Python使用pandas將表格數(shù)據(jù)進(jìn)行處理
前言
任務(wù)描述:
當(dāng)前有一份excel表格數(shù)據(jù),里面存在缺失值,需要對缺失的數(shù)據(jù)到es數(shù)據(jù)庫中進(jìn)行查找并對其進(jìn)行把缺失的數(shù)據(jù)進(jìn)行補(bǔ)全。
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ù)不能格式化換行,否則報(bào)錯(cuò)】
# 向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ù)
【注意:默認(rèn)查詢索引下的所有數(shù)據(jù)】
# 查詢索引中的所有數(shù)據(jù)
GET physical_examination/_search
{
"query": {
"match_all": {}
}
}

二、對excel表格中的數(shù)據(jù)處理操作
2.1 導(dǎo)出es查詢的數(shù)據(jù)
- 方法一:直接在kibana或postman查詢的結(jié)果中進(jìn)行復(fù)制粘貼到一個(gè)文檔。
- 方法二:使用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ù)填補(bǔ)進(jìn)去。
# 讀取需要填補(bǔ)數(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['手機(jī)號(hào)'])
if pd.isnull(bb['手機(jī)號(hào)']):
bb['手機(jī)號(hào)'] = '666'
for cc in need_data:
if cc['name'] == bb['姓名']:
bb['手機(jī)號(hào)'] = cc['phone']
data_xlsx.iloc[i, 3] = bb['手機(jī)號(hào)']
print(bb['姓名'], bb['手機(jī)號(hào)'])
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)
# 讀取需要填補(bǔ)數(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['手機(jī)號(hào)'])
if pd.isnull(bb['手機(jī)號(hào)']):
bb['手機(jī)號(hào)'] = '666'
for cc in need_data:
if cc['name'] == bb['姓名']:
bb['手機(jī)號(hào)'] = cc['phone']
data_xlsx.iloc[i, 3] = bb['手機(jī)號(hào)']
print(bb['姓名'], bb['手機(jī)號(hào)'])
print("-" * 100)
print(data_xlsx)
# 保存數(shù)據(jù)到新文件中
data_xlsx.to_excel('./data/new_data.xlsx', sheet_name='Sheet1', index=False, header=True)運(yùn)行效果,最終處理好的數(shù)據(jù)如下所示:

到此這篇關(guān)于Python使用pandas將表格數(shù)據(jù)進(jìn)行處理的文章就介紹到這了,更多相關(guān)pandas表格數(shù)據(jù)處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python光學(xué)仿真相速度和群速度計(jì)算理解學(xué)習(xí)
從物理學(xué)的機(jī)制出發(fā),波動(dòng)模型相對于光線模型,顯然更加接近光的本質(zhì);但是從物理學(xué)的發(fā)展來說,波動(dòng)光學(xué)旨在解決幾何光學(xué)無法解決的問題,可謂光線模型的一種升級(jí)2021-10-10
Pycharm以root權(quán)限運(yùn)行腳本的方法
今天小編就為大家分享一篇Pycharm以root權(quán)限運(yùn)行腳本的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
Python處理字節(jié)串:struct.pack和struct.unpack使用
這篇文章主要介紹了Python處理字節(jié)串:struct.pack和struct.unpack使用方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
Pytorch基礎(chǔ)教程之torchserve模型部署解析
torchserve是基于netty網(wǎng)絡(luò)框架實(shí)現(xiàn)的,底層使用EpollServerSocketChannel服務(wù)進(jìn)行網(wǎng)絡(luò)通信,通過epoll多路復(fù)用技術(shù)實(shí)現(xiàn)高并發(fā)網(wǎng)絡(luò)連接處理,這篇文章主要介紹了Pytorch基礎(chǔ)教程之torchserve模型部署和推理,需要的朋友可以參考下2023-07-07
Python用yield from實(shí)現(xiàn)異步協(xié)程爬蟲的實(shí)踐
本文主要介紹了Python用yield from實(shí)現(xiàn)異步協(xié)程爬蟲的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
Python實(shí)現(xiàn)時(shí)間序列變化點(diǎn)檢測功能
平穩(wěn)性是時(shí)間序列分析與預(yù)測的核心概念,在平穩(wěn)條件下,時(shí)間序列的統(tǒng)計(jì)特性(如均值)在時(shí)間維度上保持不變,僅存在隨機(jī)波動(dòng),但是時(shí)間序列通常會(huì)經(jīng)歷結(jié)構(gòu)性斷裂或變化,本文給大家介紹了Python實(shí)現(xiàn)時(shí)間序列變化點(diǎn)檢測功能,需要的朋友可以參考下2024-09-09
Python使用Flask框架同時(shí)上傳多個(gè)文件的方法
這篇文章主要介紹了Python使用Flask框架同時(shí)上傳多個(gè)文件的方法,實(shí)例分析了Python中Flask框架操作文件實(shí)現(xiàn)上傳的技巧,需要的朋友可以參考下2015-03-03

