python - python中有什么方法可以从数据框中搜索模式并提取其相应的值吗?

标签 python regex pandas dataframe

我有一个这样的数据框。

     Area of Power Cons.           Device Name  2016-09-02   2016-09-03  
0   01.11KVA VCB-I South  SJ 11 South Zone Con    32328.19     33157.12   
1   02.11KVA VCB-II Sout  SJ 11 South Zone Con    43879.94     45152.77   
2   03.11 KVA VCB-I Nort  SJ 11 North Zone Con    30132.74     29507.58   
3   04.11 KVA VCB-II Nor  SJ 11 North Zone Con    38632.45 -16448377.89   
4   05.MAIN INCOMER-I So  SJ 11 South Zone Con    31509.76     32313.60   
5   06.MAIN INCOMER-II S  SJ 11 South Zone Con    42617.22     43841.66   
6   07.MAIN INCOMER-I No  SJ 11 North Zone Con    29284.61     28673.92   
7   08.MAIN INCOMER II N  SJ 11 North Zone Con    37391.87     36786.43   
8     22.BLOW ROOM South  SJ 11 South Zone Con     1909.47      1908.40   
9     23.BLOW ROOM North  SJ 11 North Zone Con     1437.14      1369.04   
10      24.CARDING South  SJ 11 South Zone Con     3962.26      3989.94   
11      25.CARDING-North  SJ 11 North Zone Con     3970.63      3706.69   
12   26.PREPRATORY-South  SJ 11 South Zone Con     2456.72      2576.88   
13   27.PREPRATORY-North  SJ 11 North Zone Con     2372.61      2376.38   
14     28.RING FRAME 1-5  SJ 11 South Zone Con     7384.83      7595.06   
15    29.RING FRAME 6-10  SJ 11 South Zone Con     7111.55      7313.15   
16   30.RING FRAME 11-15  SJ 11 South Zone Con     6821.89      7209.26   
17   31.RING FRAME 16-20  SJ 11 South Zone Con     6049.12      6425.47   
18   32.RING FRAME 21-25  SJ 11 North Zone Con     6481.04      6070.30   
19   33.RING FRAME 26-30  SJ 11 North Zone Con     6471.42      6160.90   
20   34.RING FRAME 31-35  SJ 11 North Zone Con     6619.86      6520.43   
21   35.RING FRAME 36-40  SJ 11 North Zone Con     6200.00      6200.00   
22    35.LINC CONER 1-10  SJ 11 South Zone Con     2292.18      2348.64   
23   36.LINC CONER 11-20  SJ 11 South Zone Con     2905.96      2943.62   
24   37.LINC CONER 21-30  SJ 11 North Zone Con     2181.58      2118.38   
25   38.LINC CONER 31-40  SJ 11 North Zone Con     2752.92      2763.26 

我想搜索包含 Ring Frame 的行并获取其各自的值。

我试过这个:

df = data[15:22]

但是我想搜索名字。提前致谢。

最佳答案

您可以使用 boolean indexing带掩码,首先将所有值转换为 lower然后使用 contains :

print (df[df['Area of Power Cons.'].str.lower().str.contains('ring frame', na=False)])

apply 的另一种解决方案,但它仅在不是 NaN 值时才有效:

print (df[df['Area of Power Cons.'].apply(lambda x: 'ring frame' in x.lower())])

如果需要提取值,请使用 str.extract :

print (df['Area of Power Cons.'].str.lower().str.extract('ring frame (.*)'))

如果所有值都是大写的:

print (df['Area of Power Cons.'].str.extract('RING FRAME (.*)'))

关于python - python中有什么方法可以从数据框中搜索模式并提取其相应的值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41095576/

相关文章:

python - 根据行条件对轴 1 上的数据框进行子集化

python - Windows 上的管道镜像

python - 使用 tkinter 在 python 中播放动画 GIF

javascript - 用于匹配特定格式 IP 的正则表达式

javascript - JS RegExp 未捕获匹配项

python - 有没有办法使用正则表达式组合多个字符串?

python - Pandas 组 : usage in this instance

python - pandas - 涉及分类分组的最近值查找

python - 如何在 OS X 上进行 PyS60 开发

python - 如何在 Ipython 中显示与内联的 matplotlib 图交错的打印语句?