【问题标题】:pandas subset using sliced boolean index使用切片布尔索引的熊猫子集
【发布时间】:2017-02-27 00:11:49
【问题描述】:

制作测试数据的代码:

import pandas as pd
import numpy as np

testdf = {'date': range(10),
      'event': ['A', 'A', np.nan, 'B', 'B', 'A', 'B', np.nan, 'A', 'B'],
      'id': [1] * 7 + [2] * 3}
testdf = pd.DataFrame(testdf)

print(testdf)

给了

    date event  id
0     0     A   1
1     1     A   1
2     2   NaN   1
3     3     B   1
4     4     B   1
5     5     A   1
6     6     B   1
7     7   NaN   2
8     8     A   2
9     9     B   2

子集 testdf

df_sub = testdf.loc[testdf.event == 'A',:]
print(df_sub)
    date event  id
0     0     A   1
1     1     A   1
5     5     A   1
8     8     A   2

(注意:未重新编入索引)

创建条件布尔索引

bool_sliced_idx1 = df_sub.date < 4
bool_sliced_idx2 = (df_sub.date > 4) & (df_sub.date < 6)

我想在原始df中使用这个新索引插入条件值,比如

dftest[ 'new_column'] = np.nan
dftest.loc[bool_sliced_idx1, 'new_column'] = 'new_conditional_value'

这显然(现在)给出了错误:

pandas.core.indexing.IndexingError: Unalignable boolean Series key provided

bool_sliced_idx1 看起来像

>>> print(bool_sliced_idx1)
0     True
1     True
5    False
8    False
Name: date, dtype: bool

我尝试了testdf.ix[(bool_sliced_idx1==True).index,:],但这不起作用,因为

>>> (bool_sliced_idx1==True).index
Int64Index([0, 1, 5, 8], dtype='int64')

【问题讨论】:

    标签: python pandas indexing dataframe slice


    【解决方案1】:

    成功了

    idx = np.where(bool_sliced_idx1==True)[0]
    ## or 
    # np.ravel(np.where(bool_sliced_idx1==True))
    
    idx_original = df_sub.index[idx]
    testdf.iloc[idx_original,:]
    

    【讨论】:

      【解决方案2】:

      IIUC,您可以一次组合所有条件,而不是尝试将它们链接起来。例如,df_sub.date &lt; 4 实际上只是 (testdf.event == 'A') &amp; (testdf.date &lt; 4)。因此,您可以执行以下操作:

      # Create the conditions.
      cond1 = (testdf.event == 'A') & (testdf.date < 4)
      cond2 = (testdf.event == 'A') & (testdf.date.between(4, 6, inclusive=False))
      
      # Make the assignments.
      testdf.loc[cond1, 'new_col'] = 'foo'
      testdf.loc[cond2, 'new_col'] = 'bar'
      

      这会给你:

         date event  id new_col
      0     0     A   1     foo
      1     1     A   1     foo
      2     2   NaN   1     NaN
      3     3     B   1     NaN
      4     4     B   1     NaN
      5     5     A   1     bar
      6     6     B   1     NaN
      7     7   NaN   2     NaN
      8     8     A   2     NaN
      9     9     B   2     NaN
      

      【讨论】:

      • 是的,这是正确的,因为如果你不链接它们并从 df_sub 获取条件,你的布尔索引将不会有相同的长度,所以会出现错误!
      猜你喜欢
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      • 2018-01-13
      • 2019-10-28
      • 2017-02-22
      • 2014-11-05
      • 2021-03-19
      • 2021-08-07
      相关资源
      最近更新 更多