根据列中的值过滤数据框的行

时间:2019-09-27 05:29:29

标签: python pandas dataframe

我想过滤一个数据框的行,该行的值小于10,例如10。

import numpy as np
import pandas as pd
from pprint import pprint
df = pd.DataFrame(np.random.randint(0,100,size=(10, 4)), columns=list('ABCD'))
df = df[df <10]

给予

A   B    C    D
0  5.0 NaN  NaN  NaN
1  NaN NaN  NaN  NaN
2  0.0 NaN  6.0  NaN
3  NaN NaN  NaN  NaN
4  NaN NaN  NaN  NaN
5  6.0 NaN  NaN  NaN
6  NaN NaN  NaN  NaN
7  NaN NaN  NaN  7.0
8  NaN NaN  NaN  NaN
9  NaN NaN  NaN  NaN

预期:

0   5  57  87  95
2   0  80   6  82
5   6  33  74  75
7  71  44  60   7

关于如何获得预期结果的任何建议?

1 个答案:

答案 0 :(得分:3)

使用:

function Set-AccessOnRemoteMachine {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $True)]
        [string[]]$ComputerName,

        [Parameter(Mandatory = $True)]
        [string]$User,

        [Parameter(Mandatory = $True)]
        [String]$Group,

        [Parameter(Mandatory = $True)]
        [String]$AccessType
    )

    if (($AccessType -eq 'Grant') -and ($Group -eq 'Remote Desktop Users')) {
        foreach ($Computer in $ComputerName) {
            if ((Invoke-Command -ComputerName $Computer -ScriptBlock { HOSTNAME }) -eq "$Computer" ) {
                Invoke-Command -ComputerName $Computer -ScriptBlock { param($Group, $User) net localgroup $Group $User /add } -ArgumentList $Group, $User
                Write-Output "Successfully added the user $User in the Group $Group on the computer $Computer"
            } else {
                Write-Output "$Computer is not reachble"
            }
        }
    } elseif (($AccessType -eq 'Grant') -and ($Group -eq 'Administrators')) {
        foreach ($Computer in $ComputerName) {
            if ((Invoke-Command -ComputerName $Computer -ScriptBlock { HOSTNAME }) -eq "$Computer" ) {
                Invoke-Command -ComputerName $Computer -ScriptBlock { param($Group, $User) net localgroup $Group $User /add } -ArgumentList $Group, $User
                Write-Output "Successfully added the user $User in the Group $Group on the computer $Computer"
            } else {
                Write-Output "$Computer is not reachble"
            }
        }
    } elseif (($AccessType -eq 'Revoke') -and ($Group -eq 'Remote Desktop Users')) {
        foreach ($Computer in $ComputerName) {
            if ((Invoke-Command -ComputerName $Computer -ScriptBlock { HOSTNAME }) -eq "$Computer" ) {
                Invoke-Command -ComputerName $Computer -ScriptBlock { param($Group, $User) net localgroup $Group $User /delete } -ArgumentList $Group, $User
                Write-Output "Successfully added the user $User in the Group $Group on the computer $Computer"
            } else {
                Write-Output "$Computer is not reachble"
            }
        }
    } elseif (($AccessType -eq 'Revoke') -and $Group -eq ('Administrators')) {
        foreach ($Computer in $ComputerName) {
            if ((Invoke-Command -ComputerName $Computer -ScriptBlock { HOSTNAME }) -eq "$Computer" ) {
                Invoke-Command -ComputerName $Computer -ScriptBlock { param($Group, $User) net localgroup $Group $User /delete } -ArgumentList $Group, $User
                Write-Output "Successfully added the user $User in the Group $Group on the computer $Computer"
            } else {
                Write-Output "$Computer is not reachble"
            }
        }
    }
}

Set-AccessOnRemoteMachine -ComputerName $Computer -Group $Group -User $User -AccessType $AccessType

如果要按任何条件值进行过滤,则必须添加DataFrame.any来测试布尔值np.random.seed(21) df = pd.DataFrame(np.random.randint(0,100,size=(10, 4)), columns=list('ABCD')) 中的至少一个True

DataFrame

df1 = df[(df < 10).any(axis=1)]
print (df1)
    A   B   C   D
0  73  79  56   4
5   5  18  70  50
7   5  80  35  91
9   6  84  90  28