在尝试拍摄pandas DataFrame片段时,我一直遇到这个问题。它有四列,如下所示,我们称之为all_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
,这就是generations
手动数组的原因。
当我尝试使用相同的代码并仅更改数据帧时,似乎每个切片数据帧并删除NaN的操作都会给我一个错误Unalignable boolean Series key provided
。
同样,Channel
列可以是Online
或Branch
。同样,我试图通过all_data
列分割Channel
以从Online
或Branch
条件创建新数据帧的任何方式,我得到与布尔值相同的错误索引。
我在其他帖子(reset_index, pd.notnull,)
等尝试过很多选项,但是当我用相同的代码绘制它时,它会不断创建索引问题。
从all_data
创建未创建Unalignable boolean Series key
错误的子集的最佳方法是什么?
这在过去有效:
#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')