简体   繁体   中英

How can I extract values from a dataframe or filter based on some criteria in Python?

I have a data frame with extracted values from some files. How can I filter or extract the first two rows of data after the value u in col 1. The col 1 value will have a range of 80 that I want to capture after the value u. The value u might be two or three files after a new filex in col 0 or none at all as shown below in file3.

    0          1          2       3
0   file1   value u     file1   value u
1   file1   value u     file1   value u
2   file1   value 85    file1   th_v 5
3   file1   value 10    file1   th_v 2
4   file1   value 10    file1   th_v 4
5   file1   value 88    file1   th_v 4
6   file2   value u     file2   value u
7   file2   value 88    file2   th_v 7
8   file2   value 2     file2   th_v 4
9   file2   value 88    file2   th_v 3
10  file2   value 0     file2   th_v 1
11  file3   value 89    file3   th_v 5
12  file3   value 2     file3   th_v 5
13  file3   value 4     file3   th_v 1

output:
         0          1         2       3
    0   file1   value 85    file1   th_v 5
    1   file1   value 10    file1   th_v 2
    2   file2   value 88    file2   th_v 7
    3   file2   value 2     file2   th_v 4
    4   file3   value 89    file3   th_v 5
    5   file3   value 2     file3   th_v 5

如果可以测试列对的相同数据模式(相同的文件列组和值列的相同结尾)是否可以测试最后一个值是否为数字,然后是GroupBy.head

df = df[df[1].str.contains('\d$')].groupby(0).head(2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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