我有一个CSV文件,格式为:
BUFFER_SIZE,RUN,DURATION
1000,1,0.5
1000,2,0.62
1000,3,0.48
1000,4,0.59
2000,1,0.44
2000,2,0.35
2000,3,0.29
2000,4,0.41
...
(数据是假的,只是为了说明我的例子)
我想绘制buffer_size
vs mean(duration)
。
我可以毫无问题地分组和计算方法:
bench_results = pd.read_csv('bench_results.csv')
bench_by_size = bench_results.groupby('BUFFER_SIZE')
bench_by_size.mean()
给了我预期的结果。
plot(bench_results.groupby('BUFFER_SIZE').mean()['DURATION'])
几乎我想要的,除了我希望X轴是BUFFER_SIZE。
这很难看,但却给了我想要的东西:
Xvals = []
Yvals = []
for key, grp in bench_results.groupby(['BUFFER_SIZE']):
Xvals.append(key)
Yvals.append(mean(grp['DURATION']))
plot(Xvals, Yvals)
有没有更好的方法呢?我想避免迭代GroupBy对象。
答案 0 :(得分:1)
plt.plot(bench_by_size.mean()['DURATION'])
应该有效。例如,
import pandas as pd
import matplotlib.pyplot as plt
bench_results = pd.DataFrame(
{'BUFFER_SIZE': [1000, 1000, 1000, 1000, 2000, 2000, 2000, 2000],
'DURATION': [0.5, 0.62, 0.48, 0.59, 0.44, 0.35, 0.29, 0.41],
'RUN': [1, 2, 3, 4, 1, 2, 3, 4]})
# bench_results = pd.read_csv('data')
bench_by_size = bench_results.groupby('BUFFER_SIZE')
means = bench_by_size.mean()
plt.plot(means['DURATION'], linestyle='-', marker='o', markersize=10)
plt.xlabel(means.index.name)
plt.ylabel('DURATION')
plt.show()
产量