python-3.x - 根据列名中的匹配字符串对 pandas 单元格(字符串)进行排序

标签 python-3.x string pandas sorting

给定以下数据框:

df = pd.DataFrame({'doc' : ['2739','2697','3135','896'],
               'tool' : ["system: 15", "architectur: 5" ,"tool: 10", "tool: 11"],
               'system' : ["tool: 1", "tool: 3" , "system: 5", "system: 14"],
               'architectur' : ["architectur: 4", "system: 28", "architectur: 3", "architectur: 10"]})

df = df.set_index('doc')


print(df)
               tool      system      architectur
doc                                              
2739      system: 15     tool: 1   architectur: 4
2697  architectur: 5     tool: 3       system: 28
3135        tool: 10   system: 5   architectur: 3
896         tool: 11  system: 14  architectur: 10

我正在尝试根据列名称中的匹配字符串对字符串重新排序。

最终目标是获得这个:

          tool      system      architectur
doc                                        
2739   tool: 1  system: 15   architectur: 4
2697   tool: 3  system: 28   architectur: 5
3135  tool: 10   system: 5   architectur: 3
896   tool: 11  system: 14  architectur: 10

提前致谢!

最佳答案

我认为你可以重新构建你的数据框

yourdf=pd.DataFrame([dict(map(tuple,[y.split(':') for y in x ])) for x in (df.values.tolist())],index=df.index)
yourdf
Out[159]: 
     architectur system tool
doc                         
2739           4     15    1
2697           5     28    3
3135           3      5   10
896           10     14   11

关于python-3.x - 根据列名中的匹配字符串对 pandas 单元格(字符串)进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55093597/

相关文章:

ios - 如何从字符串中获取 NSDate

c++ - 在循环中连接 Visual Studio C++ 6.0 中的字符串

python - 使用多索引数据框生成复杂的 seaborn 点图

python-3.x - 如何从 Pandas DataFrame 绘制家谱?

python-3.x - 创建具有行名、列名和最大列值的数据框

mysql - mysql 查询的正则表达式?

python - 根据名称从多个数据框中生成唯一键

python - Pandas 系列 - 将日期范围之间的数据设置为常量

python - PyQt4:QMenu 中未显示图标

python - 如何动态地将数据从列表添加到数据库(PostgreSQL)