-7
我的csv文件非常复杂..它包含数字以及字符串属性。 这是我的CSV文件看起来像 我要绘制的过程柱状图对比的CPUID从csv文件使用matplotlib和pandas绘制直方图
我的csv文件非常复杂..它包含数字以及字符串属性。 这是我的CSV文件看起来像 我要绘制的过程柱状图对比的CPUID从csv文件使用matplotlib和pandas绘制直方图
您可以通过hist
使用read_csv
,indexing with str和情节:
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()
你可以展示你试过的东西吗?您没有发布加载数据的代码或尝试了任何有据可查的[绘图方法](http://pandas.pydata.org/pandas-docs/stable/api.html#api-dataframe-plotting) – EdChum
使用 ['read_csv()'function](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html) 将csv读取为pandas DataFrame。 – Sevanteri
如果在第二列中只有'cpu_id',那么仅仅给列标题“cpu_id”并删除(搜索/替换)除了值0/1之外的字段中的所有内容是否合理? – jDo