我希望对“ne”和“tw”进行部分字符串匹配。
基本设置:
arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']),
np.array(['one', 'two', 'three', 'four', 'five', 'six', 'nine', 'none'])]
s = pd.Series(np.random.randn(8), index=arrays) df = pd.DataFrame(np.random.randn(8, 4), index=arrays)
df Out[2]:
0 1 2 3
bar one 1.238627 0.027646 -1.707589 -0.769593
two 0.144709 0.087773 0.725266 -0.463602
baz three 2.098599 0.551828 -0.129251 1.150297
four 0.784710 1.957488 -0.919756 -0.291112
foo five 0.578707 0.292793 0.129004 -0.704882
six -0.539508 -0.301554 -0.350157 0.018169
qux nine 0.404407 -1.226800 -1.463461 -2.569753
none 0.774964 0.204157 -0.695053 -1.161872
要得到: Out[3]:
0 1 2 3
bar one -0.759341 0.979908 0.423735 0.224613
two 1.224353 -0.287837 1.020571 2.633446
qux nine 0.888379 0.773314 1.507129 -0.279791
none -0.967281 -1.239551 0.609369 -0.725862
在单个索引中,我会简单地做:df[df.index.str.contains("ne"))]
对于多个部分字符串匹配:df[df.index.str.contains('|'.join(["ne","tw"))]
选择部分字符串匹配的最佳选择是什么?恭敬地,为什么不像 Pandas 的其他部分那样对 MultiIndex 提供太多支持?谢谢!
最佳答案
您可以从 MultiIndex 中选择一个特定的索引,然后运行 .str.contains
对此:
# df.index.get_level_values(1) returns an pd.Index object
df.loc[df.index.get_level_values(1).str.contains("ne|tw")]
0 1 2 3
bar one 0.513132 -0.646786 -1.687423 2.614390
two -1.070990 1.618638 -1.485864 -0.813031
qux nine -0.438507 -0.830141 0.009474 0.206083
none -0.811970 0.342299 -0.165243 -1.482466
关于pandas - 如何在 pandas.MultiIndex 中找到部分字符串匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64232264/