Car*_*nte 2 python filtering dataframe python-3.x pandas
我有一个数据框,如果每行两列的值大于两个特定数字,我需要过滤该数据框。另外,重要的是我对数据框一无所知,除了这些列是数字 -2 和 -3
例如,我们有dataframe = pd.DataFrame(data=[[1,2,3,4],[5,6,7,8],[1,2,-3,4],[5,-6,7,8]]). 想象一下,如果 -2 和 -3 列大于 0,我们需要对其进行过滤。在这种情况下,我们将仅返回前两行。
我尝试过使用return dataframe[(dataframe[:, -3] >= 0) and (dataframe[:, -2] >= 0)],但它给了我以下错误:TypeError: unhashable type: 'slice'。
用于iloc按位置选择列并botwise and用于&:
df = dataframe[(dataframe.iloc[:, -3] >= 0) & (dataframe.iloc[:, -2] >= 0)]
print (df)
0 1 2 3
0 1 2 3 4
1 5 6 7 8
Run Code Online (Sandbox Code Playgroud)