繁体   English   中英

如何过滤python dataframe中的值?

[英]How to filter values in python dataframe?

如何根据以.开头的列符号过滤 dataframe df1 和第一位数字

    df1
    
      SYMBOL           TYPE
    .1E09UOV      Exchange code
    .2E09UP0      Exchange code
    .AT0013F      Exchange code
    .BT0013G      Exchange code
    .CT002MS      Exchange code
    .DT002MT      Exchange code
    .7T003MT      Exchange code
    .7T004MT      Exchange code
    .7T001MT      Exchange code
    .7T003MT      Exchange code
    
    
    
    Expected output
    
      SYMBOL           TYPE
    .1E09UOV      Exchange code
    .2E09UP0      Exchange code
    .7T003MT      Exchange code
    .7T004MT      Exchange code
    .7T001MT      Exchange code
    .7T003MT      Exchange code

试过的代码:

df1.loc[(df1['SYMBOL'].re.sub(r'.\d')]

您可以使用以下内容:

df1 = df1[df1['SYMBOL'].str.match('^\.[0-9].*')]
  • ^ = 字符串的开始
  • \. = 寻找时期
  • [0-9] = 寻找单个数字
  • .* = 查找零个或多个字符


这是显示完整代码的示例:

代码:

import pandas as pd

df1 = pd.DataFrame({ 'SYMBOL': ['.1E09UOV', '.2E09UP0', '.AT0013F', '.BT0013G', '.CT002MS', '.DT002MT', '.7T003MT', '.7T004MT', '.7T001MT', '.7T003MT'],
                    'TYPE': ['Exchange code', 'Exchange code', 'Exchange code', 'Exchange code', 'Exchange code', 'Exchange code', 'Exchange code', 'Exchange code', 'Exchange code', 'Exchange code']})

df1 = df1[df1['SYMBOL'].str.match('^\.[0-9].*')]

print(df1)

Output:

     SYMBOL           TYPE
0  .1E09UOV  Exchange code
1  .2E09UP0  Exchange code
6  .7T003MT  Exchange code
7  .7T004MT  Exchange code
8  .7T001MT  Exchange code
9  .7T003MT  Exchange code

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM