取消堆叠并重新排序pandas数据帧(Unstack and reorder pandas dataframe)
我已经四处寻找,但找不到解决方案,如何拆除和重新排序熊猫数据帧。
假设我有以下数据帧:
df = pd.DataFrame({'type': [1, 2, 2, 1, 2, 1, 2, 1, 2, 2], 'band': ['A', 'B', 'C', 'C', 'B', 'B', 'A', 'A', 'B', 'C'], 'val': [0.18, 0.19, 0.20, 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27]})
我可以按
'type'
和'band'
分组以获得两个维度的均值,并使用unstack()
将'band'
显示为列:df.groupby(['type', 'band']).mean().unstack(level=1)
我的问题是,如何重新排列列,以便它们处于降序(或更一般地,任意排序)的顺序?
输出如下:
val band A B C type 1 0.215 0.230000 0.210 2 0.240 0.223333 0.235
我想要的是以下内容:
val band C B A type 1 0.210 0.230000 0.215 2 0.235 0.223333 0.240
在实践中,我有超过3列。
I've searched around but couldn't find a solution for how to unstack and reorder a pandas dataframe.
Suppose I have the following dataframe:
df = pd.DataFrame({'type': [1, 2, 2, 1, 2, 1, 2, 1, 2, 2], 'band': ['A', 'B', 'C', 'C', 'B', 'B', 'A', 'A', 'B', 'C'], 'val': [0.18, 0.19, 0.20, 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27]})
I can group by
'type'
and'band'
to get the mean across both dimensions, and useunstack()
to show'band'
as columns:df.groupby(['type', 'band']).mean().unstack(level=1)
My question is, how can I reorder the columns so that they're in descending (or more generally, any arbitrarily sorted) order?
The output looks like this:
val band A B C type 1 0.215 0.230000 0.210 2 0.240 0.223333 0.235
What I want is the following:
val band C B A type 1 0.210 0.230000 0.215 2 0.235 0.223333 0.240
In practice, I have many more than 3 columns.
原文:https://stackoverflow.com/questions/47580313
最满意答案
在创建组件时,您需要发送操作应该映射到该范围的内容,在您的情况下,它们是相同的操作名称,但在其他情况下,组件可能正在调用
acceptChanges
,它可能会触及控制器操作acceptImageChanges
where - 另一个可能是acceptDocumentChanges
。 这允许您在同一范围内多次使用该组件,但是每个实例都可以自定义传递的操作。{{view App.ImportFileView invalidFileType='invalidFileType' acceptChanges='acceptChanges' }}
You need to send in what the action should map to in that scope when you create the component, in your case they are the same action name, but in other cases, the component could be calling
acceptChanges
and it could be hitting the controller actionacceptImageChanges
where-as another may beacceptDocumentChanges
. This allows you to use the component multiple times in the same scope, yet have the actions that are passed out of it be customized per instance.{{view App.ImportFileView invalidFileType='invalidFileType' acceptChanges='acceptChanges' }}
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
在你的控制器中 foos: null,// populated using set() somewhere validate: function() { var foos = this.get('foos'); foos.forEach(function(item) { // process }); }.property('foos') 在模板中不需要做任何事情,{{each}}必然会受到影响。 有关项目(foo项)上的模板更改发生更改的更详细示例,您可以使用Ember.Array ...
-
Redux FAQ中包含了该主题: http : //redux.js.org/docs/FAQ.html#reducers-share-state 。 目前,是的,答案是“编写额外的自定义reducer逻辑”,或“使用getState将更多数据放入操作”。 也就是说,目前正在讨论的一个PR会让combineReducers作为附加参数传递整个状态。 请参阅https://github.com/reactjs/redux/pull/1768 。 另外,回答Pavel Tarno的评论:是的, 如果你使用co ...
-
据我所知,StackOverflow有很多人以各种方式提出这个问题,但没有人完全回答。 我的规范要求用户能够选择电子邮件,微博,Facebook或短信,每个人都有自定义文本。 这是我如何实现的: public void onShareClick(View v) { Resources resources = getResources(); Intent emailIntent = new Intent(); emailIntent.setAction(Intent.ACTION_S ...
-
根据reducer状态更改在特定组件中调度操作(Dispatching an action in a specific component based on a reducer state change)[2023-02-05]
我确实不会在mapDispatchToProps函数中调用dispatch。 这个函数应该是“纯粹的”,只返回一个没有任何副作用的映射。 因为你知道什么动作应该触发模态,即。 当publicStages设置为特定值时,我建议以两种可能的方式来改变状态: 您可以在reducer中执行此操作。 当您更改publicStages ,reducer也可以访问模态状态,也可以更改它。 在行动中。 在更改PublicStages的操作中,根据您对publicStages的操作,调用操作来打开/关闭模式。 请注意,调用d ... -
有条件地安装由多个功能共享的组件(Conditional installation of a component that is shared by several features)[2022-08-07]
最后,我创建了一个子功能,其中包含仅包含windows-service组件的C功能,并且我使用在CostFinalize之后调度的两个MsiSetFeatureState自定义操作控制其安装状态 - 一个用于在安装功能C时将状态设置为不存在或者重新配置并设置'legacy-mode'标志,另一个设置状态为install-local,以防安装或重新配置功能C并且未设置'legacy-mode'标志。 Windows-service组件仍在功能A,功能B和功能C的此子功能之间共享。这似乎工作得很好! Event ... -
基于其他组件的某些操作更新,执行或呈现jsf组件(update, execute or render a jsf component based on some action of other component)[2022-02-18]
我可以通过做一些研究来解决这个问题,并从下面的链接中理解ajax执行和更新之间的区别 https://javaserverfaces.java.net/nonav/docs/2.1/vdldocs/facelets/index.html I could solve this by doing some research and understood the difference between ajax execute and update from following link https://javas ... -
如何根据某些条件从组件发送特定操作?(How can I send a specific action from a component based on some conditions?)[2021-11-11]
在创建组件时,您需要发送操作应该映射到该范围的内容,在您的情况下,它们是相同的操作名称,但在其他情况下,组件可能正在调用acceptChanges ,它可能会触及控制器操作acceptImageChanges where - 另一个可能是acceptDocumentChanges 。 这允许您在同一范围内多次使用该组件,但是每个实例都可以自定义传递的操作。 {{view App.ImportFileView invalidFileType='invalidFileType' acceptChanges='a ... -
将操作发送到父组件?(Send actions to parent component?)[2021-09-24]
在你the-table你的yield语句应该产生-table的上下文: {{yield this}} 。 然后,您应该使用target属性从the-row定位表。 {{#the-table as |table|}} {{#the-row selectedRow=selectedRow selectRow="selectRow" target=table}}something {{/the-row}} {{/the-table}} 示例: http : //embe ...