答案 0 :(得分:0)
您可以使用read_csv
,indexing with str并按hist
绘图:
import pandas as pd
import matplotlib.pyplot as plt
import io
temp=u"""kmem_kmalloc;{cpu_id=1}
kmem_kmalloc;{cpu_id=1}
kmem_kmalloc;{cpu_id=1}
kmem_kmalloc;{cpu_id=1}
kmem_kfree;{cpu_id=1}
kmem_kfree;{cpu_id=1}
power_cpu_idle;{cpu_id=0}
power_cpu_idle;{cpu_id=0}
power_cpu_idle;{cpu_id=3}"""
s = pd.read_csv(io.StringIO(temp), #after testing replace io.StringIO(temp) to filename
sep=";", #set separator, if sep=',' can be omited (default sep = ,)
header=None, #no header in csv
names=[None,'cpuid'], #set names of columns, (first is None because index)
index_col=0, #first column set to index
squeeze=True) #try convert DataFrame to Series
print s
kmem_kmalloc {cpu_id=1}
kmem_kmalloc {cpu_id=1}
kmem_kmalloc {cpu_id=1}
kmem_kmalloc {cpu_id=1}
kmem_kfree {cpu_id=1}
kmem_kfree {cpu_id=1}
power_cpu_idle {cpu_id=0}
power_cpu_idle {cpu_id=0}
power_cpu_idle {cpu_id=3}
Name: cpuid, dtype: object
#if max cpu <= 9, use Indexing with .str
s = s.str[-2].astype(int)
#if cpu > 9
#s= s.str.extract('(\d)', expand=False)
print s
kmem_kmalloc 1
kmem_kmalloc 1
kmem_kmalloc 1
kmem_kmalloc 1
kmem_kfree 1
kmem_kfree 1
power_cpu_idle 0
power_cpu_idle 0
power_cpu_idle 3
Name: cpuid, dtype: int32
plt.figure();
s.hist(alpha=0.5)
plt.show()