首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >请帮助为熊猫创造一个功能。

请帮助为熊猫创造一个功能。
EN

Stack Overflow用户
提问于 2022-08-18 00:37:24
回答 1查看 43关注 0票数 -1

函数dataframe,该函数以字典为输入并从字典中创建数据,对字典进行排序。

使用说明

代码语言:javascript
复制
1. Create a dataframe with the input dictionary
2. Columns should be Name Age
3. Print "Before Sorting"
4. Print a Newline
5. Print the dataframe before sorting. 
Note: Printing the dataframe must not contain index.
6. Print a Newline
7. Sort the dataframe in ascending order based on Age column
8. Print "After Sorting"
9. Print a Newline
10. Print the dataframe after sorting. Note: Printing the dataframe must not contain index.

▾样例0

样本输入

威廉:42,乔治:10,约瑟夫:22,亨利:15,塞缪尔:32,大卫:18

样本输出

分拣前

名称时代

威廉42

乔治。10

约瑟夫。22

亨利。15

塞缪尔。32

大卫。18

分拣后

名字。年龄

乔治。10

亨利。15

大卫。18

约瑟夫。22

塞缪尔。32

威廉。42

EN

回答 1

Stack Overflow用户

发布于 2022-08-18 01:00:03

希望这能有所帮助。

代码语言:javascript
复制
def dict_to_df(key, value):
    my_dict = {key:value,key:value,key:value,...}
    df = pd.DataFrame(list(my_dict.items()),columns = ['column1','column2'])
    print("Before Sorting"/n)
    print df.to_string(index=False)
    df.sort_values(by=['col2']
    print("After Sorting"/n)
    print df.to_string(index=False)'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73396262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档