Python使用pandas將表格數(shù)據(jù)進(jìn)行處理
前言
任務(wù)描述:
當(dāng)前有一份excel表格數(shù)據(jù),里面存在缺失值,需要對(duì)缺失的數(shù)據(jù)到es數(shù)據(jù)庫(kù)中進(jìn)行查找并對(duì)其進(jìn)行把缺失的數(shù)據(jù)進(jìn)行補(bǔ)全。
excel表格數(shù)據(jù)如下所示:

一、構(gòu)建es庫(kù)中的數(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 查詢(xún)數(shù)據(jù)
【注意:默認(rèn)查詢(xún)索引下的所有數(shù)據(jù)】
# 查詢(xún)索引中的所有數(shù)據(jù)
GET physical_examination/_search
{
"query": {
"match_all": {}
}
}

二、對(duì)excel表格中的數(shù)據(jù)處理操作
2.1 導(dǎo)出es查詢(xún)的數(shù)據(jù)
- 方法一:直接在kibana或postman查詢(xún)的結(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用keras實(shí)現(xiàn)非線性回歸(兩種加激活函數(shù)的方式)
這篇文章主要介紹了使用keras實(shí)現(xiàn)非線性回歸(兩種加激活函數(shù)的方式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
python算法練習(xí)之兔子產(chǎn)子(斐波那切數(shù)列)
這篇文章主要給大家介紹python算法練習(xí)兔子產(chǎn)子,文章先進(jìn)行問(wèn)題描述及分析然后設(shè)計(jì)算法最后再得出完整程序,需要的朋友可以參考一下 文章得具體內(nèi)容2021-10-10
Python使用django解決跨域請(qǐng)求的問(wèn)題
這篇文章主要給大家介紹了python如何使用django解決跨域請(qǐng)求的問(wèn)題,文中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,需要的朋友可以參考下2023-07-07
Python opencv相機(jī)標(biāo)定實(shí)現(xiàn)原理及步驟詳解
這篇文章主要介紹了Python opencv相機(jī)標(biāo)定實(shí)現(xiàn)原理及步驟詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Jmeter并發(fā)執(zhí)行Python 腳本的完整流程
這篇文章主要介紹了Jmeter并發(fā)執(zhí)行 Python 腳本的問(wèn)題詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
python中slice參數(shù)過(guò)長(zhǎng)的處理方法及實(shí)例
在本篇文章里小編給大家分享了一篇關(guān)于python中slice參數(shù)過(guò)長(zhǎng)的處理方法及實(shí)例內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。2020-12-12
python數(shù)據(jù)分析之時(shí)間序列分析詳情
這篇文章主要介紹了python數(shù)據(jù)分析之時(shí)間序列分析詳情,時(shí)間序列分析是基于隨機(jī)過(guò)程理論和數(shù)理統(tǒng)計(jì)學(xué)方法,具體詳細(xì)內(nèi)容介紹,需要的小伙伴可以參考一下2022-08-08
使用Python實(shí)現(xiàn)大學(xué)座位預(yù)約功能
這篇文章主要介紹了如何用Python實(shí)現(xiàn)大學(xué)座位預(yù)約,今天這個(gè)教程教你如何搶到座位,有座位了還怕聽(tīng)不到課嗎?感興趣的朋友一起看看吧2022-03-03

