import pandas
def order():
"""安装字典内元素 age 大小排序,再和name 大小排序
"""
text = [{‘name‘:‘dog‘,‘age‘:‘1‘,‘sex‘:‘female‘},{‘name‘:‘cat‘,‘age‘:‘2‘},{‘name‘:‘cat2‘,‘age‘:‘5‘}]
sort_by = sorted(text, key=lambda e:(e.__getitem__(‘age‘),e.__getitem__(‘name‘)))
print(sort_by)
# 变成dataframe
pandas.DataFrame(sort_by)
## 结果
[{‘name‘: ‘dog‘, ‘age‘: ‘1‘, ‘sex‘: ‘female‘}, {‘name‘: ‘cat‘, ‘age‘: ‘2‘}, {‘name‘: ‘cat2‘, ‘age‘: ‘5‘}]
原文:https://blog.51cto.com/12187730/2537093