我有一个这样的数据表
Value
Code ABCD GFHTI
Time
20100101_00:01:33.436-92.451 24 None
20100101_00:01:33.638-92.651 None 25
该表是从日志文件中获取的
logparser = parse_filter_logfile('log.txt')
df = pd.DataFrame(logparser, columns = ['Time', 'Code', 'Value'])
df.set_index(['Time', 'Code']).unstack(-1)
我使用df.pivot(index='Time', columns=['ABCD','GFHTI'])
将列更改为ABCD和GFHTI,但得到了以下错误键error:“Level ABCD not found”。你知道吗
Time ABCD GFHTI
20100101_00:01:33.436-92.451 24 None
20100101_00:01:33.638-92.651 None 25
我想要一个列名为的表,看起来像这样:
有什么办法吗?你知道吗
这是完整的代码, 日志.txt你知道吗
20100101_00:01:33.436-92.451 BLACKBOX ABCD ref 2183 value 24
20100101_00:01:33.638-92.651 BLACKBOX GFHTI ref 2183 value 25
20100101_00:01:33.817-92.851 BLACKBOX AAAA ref 2183 value 26
20100101_00:01:34.017-93.051 BLACKBOX BBBB ref 2183 value 27
and this the code:
import pandas as pd
import re
def parse_line(line):
code_pattern = r'(?<=BLACKBOX )\w+'
value_pattern = r'(?<=value )\d+'
code = re.findall(code_pattern, line)[0]
value = re.findall(value_pattern, line)[0]
ts = line.split()[0]
print (type(value))
return ts, code, value
def parse_filter_logfile(fname):
with open(fname) as f:
for line in f:
data = parse_line(line)
if data[1] in ['ABCD', 'GFHTI']:
# only yield rows that match the filter
print((data))
yield data
logparser = parse_filter_logfile('log.txt')
df = pd.DataFrame(logparser, columns = ['Time', 'Code', 'Value'])
df.set_index(['Time', 'Code']).unstack(-1)
先谢谢你。你知道吗
似乎有
MultiIndex
个列,所以使用droplevel
删除级别就可以了。你知道吗相关问题 更多 >
编程相关推荐