我有一个DataFrame,其中的列为MultiIndex,索引为名称列表,即index=['Andrew', 'Bob', 'Calvin',...]
。
我想创建一个函数来返回使用名称“ Bob”或以字母“ A”开头或以小写字母开头的数据框的所有行。如何才能做到这一点?
我df.filter()
使用regex参数调查了,但失败了,我得到了:
df.filter(regex='a')
TypeError: expected string or buffer
要么:
df.filter(regex=('a',1)
TypeError: first argument must be string or compiled pattern
我尝试了其他事情,如re.compile('a')
无济于事。
也许可以通过使用列表理解和.ix尝试不同的方法:
import pandas as pd
df = pd.DataFrame(range(4),index=['Andrew', 'Bob', 'Calvin','yosef'])
df.ix[[x for x in df.index if x=='Bob']]
df.ix[[x for x in df.index if x[0]=='A']]
df.ix[[x for x in df.index if x.islower()]]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句