python pandas并排重新排序柱以进行计数
我有一个名为by_country的数据框。在此DF中,我有多个列,上半场是按年龄按年龄的答案总数,另一半是“是”的答案次数。 我将为每个不同年龄段的人并排重新排序总计和“是”答案。是否可以轻松地执行此操作而不手动键入列?
这是我的列:
by_country.columns = ['count_18-24', 'count_25-34', 'count_35-44', 'count_45-54',
'count_55-64', 'count_65', 'count_no_reply', 'count_18_under',
'sum_18-24', 'sum_25-34', 'sum_35-44', 'sum_45-54', 'sum_55-64',
'sum_65', 'sum_no_reply', 'sum_18_under']
我想订购,就像
by_country.columns = ['count_18-24','sum_18-24', 'count_25-34','sum_25-34', 'count_35-44', 'sum_35-44','count_45-54','sum_45-54', 'count_55-64','sum_55-64','count_65', 'sum_65','count_no_reply', 'sum_no_reply', 'count_18_under','sum_18_under']
我在其他较大的数据框架中遇到类似问题,并且很难全部键入它们。任何建议和建议将受到高度赞赏!
I have a DataFrame called by_country. In this DF, I have multiple columns, first half is the total number of answers by age, the other half is the number of answers 'yes' by age.
I am going to reorder the total and 'yes' answer side by side for every different age group. Is there easier way to do this without typing columns manually?
Here is my columns:
by_country.columns = ['count_18-24', 'count_25-34', 'count_35-44', 'count_45-54',
'count_55-64', 'count_65', 'count_no_reply', 'count_18_under',
'sum_18-24', 'sum_25-34', 'sum_35-44', 'sum_45-54', 'sum_55-64',
'sum_65', 'sum_no_reply', 'sum_18_under']
I want to order like
by_country.columns = ['count_18-24','sum_18-24', 'count_25-34','sum_25-34', 'count_35-44', 'sum_35-44','count_45-54','sum_45-54', 'count_55-64','sum_55-64','count_65', 'sum_65','count_no_reply', 'sum_no_reply', 'count_18_under','sum_18_under']
I have similar problem with other bigger DataFrame and it is hard to type them all. Any suggestion and advice is highly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎您只能使用
排序
和一个键进行最后_
分隔项目:Seems like you can just sort the columns using
sorted
and a key that takes the last_
-separated item: