我有一个高度嵌套的json文件,该文件已拉平并得到如下输出,其中一行包含一列并附加了数值。有什么办法可以将其删除并按输出显示为行
输入文件
102_ip_addr, 102_ip_family, 102_ip_mask_addr, 102_email, 102_failed_attempts,103_ip_addr, 103_ip_family, 103_ip_mask_addr, 103_email, 103_failed_attempts,
3705824725, 2, 4294967295, abc@xyz.com, 0,3705824825, 4, 4294967625, sdf@xyz.com, 0
输出:
ip_addr, ip_family, ip_mask_addr, email, failed_attempts
3705824725, 2, 4294967295, abc@xyz.com, 0
3705824825, 4, 4294967625, sdf@xyz.com, 0
答案 0 :(得分:2)
如果每个新行的宽度都固定为5,则可以使用reshape
pd.DataFrame(df.values.reshape(-1,5),columns=['addr','family','mask_addr','email','attempts'])
Out[580]:
addr family mask_addr email attempts
0 3705824725 2 4294967295 abc@xyz.com 0
1 3705824825 4 4294967625 sdf@xyz.com 0
更新
df.columns=df.columns.str.split('_',1).str[1]
df.melt().assign(newrow=lambda x : x.groupby(x['variable']).cumcount() ).pivot('newrow','variable','value')
Out[596]:
variable email failed_attempts ip_addr ip_family ip_mask_addr
newrow
0 abc@xyz.com 0 3705824725 2 4294967295
1 sdf@xyz.com 0 3705824825 4 4294967625