Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 数据帧隔离关键字下的行_Python_Python 3.x_Python 2.7_Pandas - Fatal编程技术网

Python 数据帧隔离关键字下的行

Python 数据帧隔离关键字下的行,python,python-3.x,python-2.7,pandas,Python,Python 3.x,Python 2.7,Pandas,我有一个1列数据框 df = pd.read_csv(txt_file, header=None) 我试图在列中搜索字符串,然后返回后面的行 key_word_df = df[df[0].str.contains("KeyWord")] 我不知道如何才能在每次找到关键字时,将其下方的行隔离并分配给新的df。您可以使用shift函数。这里有一个例子 import pandas as pd df = pd.DataFrame({'word': ['hello', 'ice', 'kitten'

我有一个1列数据框

df = pd.read_csv(txt_file, header=None)
我试图在列中搜索字符串,然后返回后面的行

key_word_df = df[df[0].str.contains("KeyWord")]

我不知道如何才能在每次找到关键字时,将其下方的行隔离并分配给新的df。

您可以使用shift函数。这里有一个例子

import pandas as pd

df = pd.DataFrame({'word': ['hello', 'ice', 'kitten', 'hello', 'foo', 'bar', 'hello'],
                  'val': [1,2,3,4,5,6,7]})

    val word
0   1   hello
1   2   ice
2   3   kitten
3   4   hello
4   5   foo
5   6   bar
6   7   hello

keyword = 'hello'
df[(df['word']==keyword).shift(1).fillna(False)]

    val word
1   2   ice
4   5   foo

您可以使用shift功能。这里有一个例子

import pandas as pd

df = pd.DataFrame({'word': ['hello', 'ice', 'kitten', 'hello', 'foo', 'bar', 'hello'],
                  'val': [1,2,3,4,5,6,7]})

    val word
0   1   hello
1   2   ice
2   3   kitten
3   4   hello
4   5   foo
5   6   bar
6   7   hello

keyword = 'hello'
df[(df['word']==keyword).shift(1).fillna(False)]

    val word
1   2   ice
4   5   foo
这里有一个方法

获取符合条件的行的索引。然后使用
.loc
获取匹配索引+1

考虑以下示例:

df = pd.DataFrame({0: ['KeyWord', 'foo', 'bar', 'KeyWord', 'blah']})
print(df)
#         0
#0  KeyWord
#1      foo
#2      bar
#3  KeyWord
#4     blah
应用掩码,并获取索引+1的行:

key_word_df = df.loc[df[df[0].str.contains("KeyWord")].index + 1, :]
print(key_word_df)
#      0
#1   foo
#4  blah
这里有一个方法

获取符合条件的行的索引。然后使用
.loc
获取匹配索引+1

考虑以下示例:

df = pd.DataFrame({0: ['KeyWord', 'foo', 'bar', 'KeyWord', 'blah']})
print(df)
#         0
#0  KeyWord
#1      foo
#2      bar
#3  KeyWord
#4     blah
应用掩码,并获取索引+1的行:

key_word_df = df.loc[df[df[0].str.contains("KeyWord")].index + 1, :]
print(key_word_df)
#      0
#1   foo
#4  blah

您可以在索引器上使用
.shift
方法。我已经将其拆分为多行以演示正在发生的事情,但是为了实践中的简洁性,您可以在一行中进行操作

import pandas as pd
# 1. Dummy DataFrame with strings
In [1]: df = pd.DataFrame(["one", "two", "one", "two", "three"], columns=["text",])

# 2. Create the indexer, use `shift` to move the values down one and `fillna` to remove NaN values
In [2]: idx = df["text"].str.contains("one").shift(1).fillna(False)
In [3]: idx
Out [3]:
0    False
1     True
2    False
3     True
4    False
Name: text, dtype: bool

# 3. Use the indexer to show the next row from the matched values:
In: [4] df[idx]
Out: [4]
text
1  two
3  two

您可以在索引器上使用
.shift
方法。我已经将其拆分为多行以演示正在发生的事情,但是为了实践中的简洁性,您可以在一行中进行操作

import pandas as pd
# 1. Dummy DataFrame with strings
In [1]: df = pd.DataFrame(["one", "two", "one", "two", "three"], columns=["text",])

# 2. Create the indexer, use `shift` to move the values down one and `fillna` to remove NaN values
In [2]: idx = df["text"].str.contains("one").shift(1).fillna(False)
In [3]: idx
Out [3]:
0    False
1     True
2    False
3     True
4    False
Name: text, dtype: bool

# 3. Use the indexer to show the next row from the matched values:
In: [4] df[idx]
Out: [4]
text
1  two
3  two
您可以执行shift(),。默认值为1:-)您可以执行shift(),。默认值为1:-)