如何使用系列过滤数据框

问题描述 投票:1回答:1

我有一个具有以下内容的熊猫系列。

$ import pandas as pd
$ s = pd.Series(
    data = [True, False, True, True],
    index = ['A', 'B', 'C', 'D']
    )
$ s.index.name = 'my_id'

$ print(s)

my_id
A     True
B    False
C     True
D     True
dtype: bool

和类似的DataFrame。

$ df = pd.DataFrame({
    'A': [1, 2, 9, 4],
    'B': [9, 6, 7, 8],
    'C': [10, 91, 32, 13],
    'D': [43, 12, 7, 9]
})

$ print(df)

   A  B   C   D
0  1  9  10  43
1  2  6  91  12
2  9  7  32   7
3  4  8  13   9

s具有ABCD作为其索引。 df的列名也具有ABCD

True中的[s]表示将保留df中的相应列。 False中的s表示将删除df中的相应列。

如何使用s删除B列来生成另一个DataFrame?

我是说我想使用sdf创建以下DataFrame。

   A   C   D
0  1  10  43
1  2  91  12
2  9  32   7
3  4  13   9
python pandas dataframe
1个回答
1
投票

boolean indexing一起使用boolean indexingDataFrame.loc表示过滤所有行。列由填充有布尔值的DataFrame.loc过滤-:

Series
© www.soinside.com 2019 - 2024. All rights reserved.