pandas处理excel-获取小区下的prru数量

pandas处理excel-获取小区下的prru数量

输入是两个文件'NRCellDU.csv'和'SectorFunction.csv',第一个文件存放的小区信息,第二个文件存放的prru信息,将两个文件联系起来的是网元id(ManagedElement)和SF id,通过提取cell表的网元id+sf和prru表的网元id+sf找到对应的refPrruTxRxGroup。最终获取的结果是特定小区对应的PRRU数量的关系。

# -*- 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'

# 读取小区和扇区功能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()


# 数据清洗,将cell和prru两个文件,写入一个新文件“cell-prru.csv”
if os.path.exists(filename3):
    os.remove(filename3)

wf3 = open(filename3, 'a', newline='')
# 这个newling=''是为了规避直接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列,通过cell表的网元id+sf和prru表的网元id+sf找到对应的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...')

你可能感兴趣的:(pandas处理excel-获取小区下的prru数量)