DataFrame中的条件选择| 熊猫数据框

DataFrame中的条件选择 (Conditional selection in the DataFrame)

Consider the following example,

考虑以下示例,

import numpy as np
import pandas as pd
from numpy.random import randn

np.random.seed(102)
df = pd.DataFrame(randn(5,4),['P','Q','R','S','T'],['A','B','C','D'])
print(df)

Output

输出量

          A         B         C         D
P  1.668068  0.925862  1.057997 -0.920339
Q  1.299748  0.331183 -0.509845 -0.903099
R -0.130016 -2.238203  0.973165 -0.024185
S -0.484928 -1.109264 -0.558975  1.042387
T -1.712263  0.136120 -0.464444  0.050980

If we use < symbol on a DataFrame, like >0, the values in the dataFrame is compared against 0 and returned with True/False.

如果我们在DataFrame上使用<符号,例如> 0 ,则将dataFrame中的值与0进行比较,并以True / False返回。

print(df > 0)

'''
Output:
       A      B      C      D
P   True   True   True  False
Q   True   True  False  False
R  False  False   True  False
S  False  False  False   True
T  False   True  False   True
'''

Now, assign the df>0 to a Boolean value called bool_df

现在,将df> 0分配给布尔值bool_df

bool_df = df > 0
print(bool_df)

'''
Output:
       A      B      C      D
P   True   True   True  False
Q   True   True  False  False
R  False  False   True  False
S  False  False  False   True
T  False   True  False   True
'''

Pass bool_df to df, in the below we can see that the values which were True have their original value and where it is False, we have a NAN. Using this approach, we can use the conditional selection in dataFrame.

将bool_df传递给df ,在下面我们可以看到,值为True的具有原始值,如果值为False,则具有NAN。 使用这种方法,我们可以在dataFrame中使用条件选择。

print(df[bool_df])

'''
Output:
          A         B         C         D
P  1.668068  0.925862  1.057997       NaN
Q  1.299748  0.331183       NaN       NaN
R       NaN       NaN  0.973165       NaN
S       NaN       NaN       NaN  1.042387
T       NaN  0.136120       NaN  0.050980


你可能感兴趣的:(python,java,索引,算法,ubuntu)