微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – 使用Pandas排序不需要的DataFrame

考虑一个基本数据框(使用Pandas):

testDf = pandas.DataFrame({'c':[1,2],'b':[2,2],'a':[3,4]})

结果给出:

   a  b  c
0  3  2  1
1  4  2  2

代替:

   c  b  a
0  1  2  3
1  2  2  4

为什么按字母顺序排序?我想要第二个结果.

解决方法:

您需要将参数列添加DataFrame构造函数,因为dict是无序的:

print (pd.DataFrame({'c':[1,2],'b':[2,2],'a':[3,4]}, columns=['c','b','a']))
   c  b  a
0  1  2  3
1  2  2  4

Dataframe

Along with the data, you can optionally pass index (row labels) and columns (column labels) arguments. If you pass an index and / or columns, you are guaranteeing the index and / or columns of the resulting DataFrame. Thus, a dict of Series plus a specific index will discard all data not matching up to the passed index.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐