【发布时间】:2014-09-16 15:18:47
【问题描述】:
我有一个类似这样的索引 pandas DataFrame:
import pandas as pd
df = pd.DataFrame({'type':['good','good','bad'], 'nr':[0,1,2], 'value':[1,2,3]})
df.set_index(['type','nr'], inplace=True)
df
Out[153]:
| value
type nr |
--------+-------
good 0 | 1
1 | 2
--------+-------
bad 2 | 3
我想将值(使用 matplotlib)与指标“好”/“坏”一起绘制。对于这个指标,0/1 numpy 数组就足够了——0 表示数据“坏”,1 表示数据“好”。但是数据集非常大,我更喜欢生成器,而不是实际的数组。
我不知道如何从df 创建这个数组/生成器。我需要这样使用它:
import matplotlib.pyplot as plt
plt.figure()
plt.plot(good_or_bad_indicator)
plt.plot(df)
plt.show()
有人可以帮忙吗?
PS:我更喜欢基于 lambda 函数的解决方案,因为在我的实际案例中,“类型”列中有两个以上的值,我可能想要设计一个更复杂的指标来绘制。
【问题讨论】:
-
总是有
df.iteritems(),但我怀疑DataFrame的内存使用与绘图相比可以忽略不计。 -
@TomAugspurger:我试过了,但
iteritems()只返回一对(column_name, series_instance)。我不知道如何使用它来解决我的问题。
标签: python pandas dataframe multi-index