Python 3.x 提供了不可对齐的布尔级数键

Python 3.x 提供了不可对齐的布尔级数键,python-3.x,pandas,indexing,slice,Python 3.x,Pandas,Indexing,Slice,我在尝试对熊猫数据帧进行切片时一直遇到这个问题。它有四列,如下所示,我们称之为all\u data: 我可以用下面的代码很好地绘制它: generations = ['Boomers+', 'Gen X', 'Millennials', 'Students', 'Gen Z'] fig = plt.figure(figsize = (15,15)) for i,generations in enumerate(generations): ax = plt.subplot(2,3,i

我在尝试对熊猫数据帧进行切片时一直遇到这个问题。它有四列,如下所示,我们称之为
all\u data

我可以用下面的代码很好地绘制它:

generations = ['Boomers+', 'Gen X', 'Millennials', 'Students', 'Gen Z']

fig = plt.figure(figsize = (15,15))

for i,generations in enumerate(generations):
    ax = plt.subplot(2,3,i+1)
    ix = all_data['CustomerGroups'] == generations

    kmf.fit(T[ix], C[ix], label=generations)


    kmf.plot(ax=ax, legend=False)
    plt.title(generations, size = 16)
    plt.xlabel('Timeline in years')
    plt.xlim(0,5)
    if  i ==0:
        plt.ylabel('Frac Remaining After $n$ Yrs')
    if  i ==3:
        plt.ylabel('Frac Remaining After $n$ Yrs')

fig.tight_layout()
#fig.suptitle('Survivability of Checking Accts By Generation', size = 16)
fig.subplots_adjust(top=0.88, hspace = .4)
plt.show()
然而,我想做一些类似的事情。
CustomerGroups
列中有
NaN
,这是手动排列
代的原因

似乎每次对数据帧进行切片并删除NaN的操作都会给我一个错误
当我尝试打印时提供的不可对齐布尔序列键
正在使用相同的代码并只是更改数据帧

同样,
频道
列可以是
在线
分支
。同样,无论我试图通过
通道
列拆分
所有数据
,以从
联机
分支
标准创建新的数据帧,我都会得到与布尔索引相同的错误

我在这里尝试了其他帖子中的许多选项
(reset\u index,pd.notnull,)
等等,但当我用相同的代码绘制时,它会不断产生索引问题

all\u数据
创建子集的最佳方法是什么?该数据不创建
不可对齐的布尔序列键
错误

这在过去是行之有效的:

#create the slice using the .copy
online = checkingRed[['Channel', 'State', 'CustYears', 'Observed',
'CustomerGroups', 'ProductYears']].copy()

#remove the branch from the data set
online = online.loc[online['Channel'] != 'Branch'] #use .loc for cleaner slice

#reset the index so that it is unique
online['index'] = np.arange(len(online))
online = online.set_index('index')

online['index']=np.arange(len(online))
此处
len(online)
返回列数或列数,您希望
online['index']=np.arange(online.shape[0])
这可能与您的问题有关,也可能与您的问题无关,因为我对熊猫一无所知,您的缩进被破坏了。enumerate(generations)中的
for i,generations:
行看起来有问题,因为在for循环之后,generations将引用原始generations list.oops中的最后一项,刚刚注意到。谢谢,但这不是问题所在,因为在进行任何切片之前,代码都能在数据帧上完美工作。我在@EdChum处做了更改,但仍然得到错误。我正在使用Pandas v 0.18.1,如果这很重要的话。有了上面的代码,
online.index.的结果是唯一的
返回
True
。我必须有你的实际数据和完整的代码才能完全调试这个
online['index']=np.arange(len(online))
这里
len(online)
返回你想要的
online['index']=np.arange(online.shape[0])
这可能与您的问题有关,也可能与您的问题无关,因为我对熊猫一无所知,而且您的缩进被破坏。enumerate(世代)中I的
代数:
行看起来有问题,因为在for循环之后,代将引用原始代列表中的最后一项。oops,刚刚注意到。谢谢,但这不是问题,因为代码在任何切片完成之前都能在数据帧上完美工作。我在@EdChum处做了更改,但仍然得到错误。我使用的是Pandas v 0.18.1如果这很重要。对于上面的代码,
online.index.is_unique
的结果将返回
True
。我必须拥有您的实际数据和完整的代码才能完全调试它