根据一列的连续值获取数据框的行
Get the rows of dataframe based on the consecutive values of one column
有没有办法根据特定列的值获取连续的行?
例如:
column1
column2
View
row1
1
2
c
row2
3
4
a
row3
5
6
p
row4
7
8
p
row5
9
10
n
我需要获取包含单词 'app' 字母的行作为视图,所以在这个例子中我需要将 row2、row3 和 row4 保存在一个列表。
不是 pythonic 方式,而是做工作:
keep = []
for i in range(len(df) - 2):
if (df.View[i]=='a') & (df.View[i+1] =='p') & (df.View[i+2] =='p'):
keep.append(df[i])
keep.append(df[i+1])
keep.append(df[i+2])
结果:
这是一个通用的方法。我使用 index_slice_by_substring()
生成表示开始行和结束行的整数元组。函数 rows_by_consecutive_letters()
获取您的数据框、要检查的列名以及您要查找的字符串,对于 return 值,它利用 .iloc
获取 [= 的一部分24=] 按整数值计算。
获取切片索引的关键是使用 ''.join(df[column])
将“View”列值连接成一个字符串,并从左到右检查与条件字符串长度相同的子字符串,直到匹配
def index_slice_by_substring(full_string, substring) -> tuple:
len_substring = len(substring)
len_full_string = len(full_string)
for x0, x1 in enumerate(range(len_substring,len_full_string)):
if full_string[x0:x1] == substring:
return (x0,x1)
def rows_by_consecutive_letters(df, column, condition) -> pd.DataFrame:
row_begin, row_end = index_slice_by_substring(''.join(df[column]), condition)
return df.iloc[row_begin:row_end,:]
print(rows_by_consecutive_letters(your_df,"View","app"))
Returns:
column1 column2 View
1 3 4 a
2 5 6 p
3 7 8 p
您可以使用 str.find
,但这只会找到第一次出现的搜索字词。
search = 'app'
i = ''.join(df.View).find(search)
if i>-1:
print(df.iloc[i: i+len(search)])
输出
column1 column2 View
row2 3 4 a
row3 5 6 p
row4 7 8 p
要查找 none(无需错误检查),您可以使用 re.finditer
。结果是数据帧切片列表。
import re
search='p' # searched for 'p' to find more than one
[df.iloc[x.start():x.end()] for x in re.finditer(search, ''.join(df.View))]
输出
[ column1 column2 View
row3 5 6 p,
column1 column2 View
row4 7 8 p]
有没有办法根据特定列的值获取连续的行? 例如:
column1 | column2 | View | |
---|---|---|---|
row1 | 1 | 2 | c |
row2 | 3 | 4 | a |
row3 | 5 | 6 | p |
row4 | 7 | 8 | p |
row5 | 9 | 10 | n |
我需要获取包含单词 'app' 字母的行作为视图,所以在这个例子中我需要将 row2、row3 和 row4 保存在一个列表。
不是 pythonic 方式,而是做工作:
keep = []
for i in range(len(df) - 2):
if (df.View[i]=='a') & (df.View[i+1] =='p') & (df.View[i+2] =='p'):
keep.append(df[i])
keep.append(df[i+1])
keep.append(df[i+2])
结果:
这是一个通用的方法。我使用 index_slice_by_substring()
生成表示开始行和结束行的整数元组。函数 rows_by_consecutive_letters()
获取您的数据框、要检查的列名以及您要查找的字符串,对于 return 值,它利用 .iloc
获取 [= 的一部分24=] 按整数值计算。
获取切片索引的关键是使用 ''.join(df[column])
将“View”列值连接成一个字符串,并从左到右检查与条件字符串长度相同的子字符串,直到匹配
def index_slice_by_substring(full_string, substring) -> tuple:
len_substring = len(substring)
len_full_string = len(full_string)
for x0, x1 in enumerate(range(len_substring,len_full_string)):
if full_string[x0:x1] == substring:
return (x0,x1)
def rows_by_consecutive_letters(df, column, condition) -> pd.DataFrame:
row_begin, row_end = index_slice_by_substring(''.join(df[column]), condition)
return df.iloc[row_begin:row_end,:]
print(rows_by_consecutive_letters(your_df,"View","app"))
Returns:
column1 column2 View
1 3 4 a
2 5 6 p
3 7 8 p
您可以使用 str.find
,但这只会找到第一次出现的搜索字词。
search = 'app'
i = ''.join(df.View).find(search)
if i>-1:
print(df.iloc[i: i+len(search)])
输出
column1 column2 View
row2 3 4 a
row3 5 6 p
row4 7 8 p
要查找 none(无需错误检查),您可以使用 re.finditer
。结果是数据帧切片列表。
import re
search='p' # searched for 'p' to find more than one
[df.iloc[x.start():x.end()] for x in re.finditer(search, ''.join(df.View))]
输出
[ column1 column2 View
row3 5 6 p,
column1 column2 View
row4 7 8 p]