pandas處理excel-獲取小區(qū)下的prru數(shù)量
輸入是兩個(gè)文件'NRCellDU.csv'和'SectorFunction.csv'唱矛,第一個(gè)文件存放的小區(qū)信息罚舱,第二個(gè)文件存放的prru信息井辜,將兩個(gè)文件聯(lián)系起來(lái)的是網(wǎng)元id(ManagedElement)和SF id,通過(guò)提取cell表的網(wǎng)元id+sf和prru表的網(wǎng)元id+sf找到對(duì)應(yīng)的refPrruTxRxGroup管闷。最終獲取的結(jié)果是特定小區(qū)對(duì)應(yīng)的PRRU數(shù)量的關(guān)系粥脚。
# -*- encoding=UTF-8 -*-
__author__ = 'wjj1982'
__date__ = '2019/7/25 13:42'
__product__ = 'PyCharm'
__filename__ = 'cell-prru'
import pandas as pd
import csv, re, os
filename1 = 'NRCellDU.csv'
filename2 = 'SectorFunction.csv'
filename3 = 'cell-prru.csv'
# 讀取小區(qū)和扇區(qū)功能CSV
filename1 = open(filename1, 'rb')
pd_csv1 = pd.read_csv(filename1)
filename1.close()
filename2 = open(filename2, 'rb')
pd_csv2 = pd.read_csv(filename2)
filename2.close()
# 數(shù)據(jù)清洗,將cell和prru兩個(gè)文件包个,寫入一個(gè)新文件“cell-prru.csv”
if os.path.exists(filename3):
os.remove(filename3)
wf3 = open(filename3, 'a', newline='')
# 這個(gè)newling=''是為了規(guī)避直接writerow寫入總是多一行空白
w3 = csv.writer(wf3)
columns = ['ManagedElement', 'cellId', 'userLabel', 'refSectorCarrier', 'refPrruTxRxGroup', 'prru_num']
w3.writerow(columns)
wf3.close()
# 處理refsector列刷允,提取出SF字段
refSectorCarrier = []
for i in pd_csv1['refSectorCarrier']:
if '-BF' in i:
refSectorCarrier.append(i.split('er=')[1].split('-BF')[0])
else:
refSectorCarrier.append(i.split('er=')[1])
# 處理refPrruTxRxGroup列,通過(guò)cell表的網(wǎng)元id+sf和prru表的網(wǎng)元id+sf找到對(duì)應(yīng)的refPrruTxRxGroup
refPrruTxRxGroup = []
prrunum = []
for i in range(len(pd_csv1['ManagedElement'])):
for j in range(len(pd_csv2['ManagedElement'])):
if pd_csv2['ManagedElement'][j] == pd_csv1['ManagedElement'][i] and pd_csv2['moId'][j] == refSectorCarrier[i]:
refPrruTxRxGroup.append(pd_csv2.iloc[j]['refPrruTxRxGroup'])
prrunum.append(pd_csv2.iloc[j]['refPrruTxRxGroup'].count('Group-'))
break
# 按列寫入新csv
pd_csv3 = pd.read_csv(filename3)
pd_csv3['ManagedElement'] = pd_csv1['ManagedElement']
pd_csv3['cellId'] = pd_csv1['cellId']
pd_csv3['userLabel'] = pd_csv1['userLabel']
pd_csv3['refSectorCarrier'] = refSectorCarrier
pd_csv3['refPrruTxRxGroup'] = refPrruTxRxGroup
pd_csv3['prru_num'] = prrunum
print("succeed, you can check cell-prru.csv now!")
input('Press Enter to exit...')