python pandas的dataframe 数据重构

数据:mr数据,每个采样点有主服务小区以及7个邻区的eci、rsrp、mr_count信息;

需求:需要将邻区独立生成一个采样点,处理后采样点基本就是原数据的7倍

大概就是这样子:

python pandas的dataframe 数据重构_第1张图片


使用stack/unstack可以很方便地进行行列转换,但想要的实现的要比行列转化更复杂些。不过使用pandas来处理也比较简单

1、读取数据:

t = pd.read_csv('./fastPg.csv',delimiter= ",",encoding='gbk')
t.columns=[x.lower() for x in t.columns]

output:

python pandas的dataframe 数据重构_第2张图片

2、提取小区数据,t1~t7代表7个最强邻区:

scell=t[['longitude','latitude','eci','mr_count','avg_rsrp']]
for i in range(1,8):
    exec("t{0}=t[['longitude','latitude','t{0}_ecgi','t{0}_count','t{0}_rsrp']]".format(i))

t1,output:

python pandas的dataframe 数据重构_第3张图片


3、把字段名统一并使用concat函数连接

for df in [scell,t1,t2]:
    df.columns=['longitude','latitude','eci','mr_count','avg_rsrp']
df=pd.concat([scell,t1,t2])
df

df, output:

python pandas的dataframe 数据重构_第4张图片

你可能感兴趣的:(python,数据处理)