Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.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 查找与中的条件匹配的第n行_Python_Pandas_Dataframe_Pattern Matching - Fatal编程技术网

Python 查找与中的条件匹配的第n行

Python 查找与中的条件匹配的第n行,python,pandas,dataframe,pattern-matching,Python,Pandas,Dataframe,Pattern Matching,假设我有以下数据帧: df = pd.DataFrame(np.array(my_list).reshape(3,3), columns = list("abc")) print (df) a b c 0 1 2 3 1 4 5 6 2 4 8 9 3 0 1 0 如何仅删除与条件匹配的行的第n个实例,例如df['a']==4的第二个实例 在这种情况下,结果应为: a b c 0 1 2 3 1 4 5 6 3 0 1 0 您可以

假设我有以下数据帧:

df = pd.DataFrame(np.array(my_list).reshape(3,3), columns = list("abc"))
print (df)
   a  b  c
0  1  2  3
1  4  5  6
2  4  8  9
3  0  1  0
如何仅删除与条件匹配的行的第n个实例,例如df['a']==4的第二个实例

在这种情况下,结果应为:

   a  b  c
0  1  2  3
1  4  5  6
3  0  1  0

您可以获得表达式df['a']==4的第n个真值的索引

在哪里,

df['a'] == 4

0    False
1     True
2     True
3    False
Name: a, dtype: bool
以及

df['a'] == 4

0    False
1     True
2     True
3    False
Name: a, dtype: bool
df.index[(df['a'] == 4)]
# Int64Index([1, 2], dtype='int64')