简体   繁体   English

添加到多索引数据帧时如何保持词法排序状态?

[英]How to maintain lexsort status when adding to a multi-indexed DataFrame?

Say I construct a dataframe with pandas, having multi-indexed columns:假设我用 Pandas 构建了一个具有多索引列的数据框:

mi      = pd.MultiIndex.from_product([['trial_1', 'trial_2', 'trial_3'], ['motor_neuron','afferent_neuron','interneuron'], ['time','voltage','calcium']])
ind     = np.arange(1,11)
df      = pd.DataFrame(np.random.randn(10,27),index=ind, columns=mi)

Link to image of output dataframe链接到输出数据帧的图像

Say I want only the voltage data from trial 1. I know that the following code fails, because the indices are not sorted lexically:假设我只想要来自试验 1 的电压数据。我知道以下代码失败,因为索引没有按词法排序:

idx    = pd.IndexSlice
df.loc[:,idx['trial_1',:,'voltage']]

As explained in another post , the solution is to sort the dataframe's indices, which works as expected:另一篇文章所述,解决方案是对数据框的索引进行排序,这按预期工作:

dfSorted    = df.sortlevel(axis=1)
dfSorted.loc[:,idx['trial_1',:,'voltage']]

I understand why this is necessary.我明白为什么这是必要的。 However, say I want to add a new column:但是,假设我想添加一个新列:

dfSorted.loc[:,('trial_1','interneuron','scaledTime')] = 100 * dfSorted.loc[:,('trial_1','interneuron','time')]

Now dfSorted is not sorted anymore, since the new column was tacked onto the end, rather than snuggled into order.现在dfSorted不再排序,因为新列被添加到末尾,而不是依序排列。 Again, I have to call sortlevel before selecting multiple columns.同样,我必须在选择多个列之前调用sortlevel

I feel this makes for repetitive, bug-prone code, especially when adding lots of columns to the much bigger dataframe in my own project.我觉得这会产生重复的、容易出错的代码,尤其是在我自己的项目中向更大的数据框添加大量列时。 Is there a (preferably clean-looking) way of inserting new columns in lexical order without having to call sortlevel over and over again?是否有一种(最好是看起来干净的)方法可以按词法顺序插入新列,而不必一遍又一遍地调用 sortlevel?

One approach would be to use filter which does a text filter on the column names:一种方法是使用filter ,其确实在列名称的文本过滤器:

In [117]: df['trial_1'].filter(like='voltage')
Out[117]:
   motor_neuron afferent_neuron interneuron
        voltage         voltage     voltage
1     -0.548699        0.986121   -1.339783
2     -1.320589       -0.509410   -0.529686

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM