python - DataFrame need help to organise columns and rows of a DF
问题描述
I have a rather simple DataFrame but problems appear when I try to reorganize it to provide a specific rows and columns order for another application.
I tried using MultiIndexing but I failed to solve the complexity
import pandas as pd
pd.DataFrame([('adc','20030103','USD','KLP','0.41'),
('adc','20030103','USD','TRF','0.59'),
('adc','20030104','USD','UTC','0.35'),
('adc','20030104','USD','ZSE','0.65')],
columns=('pfl','date','cur','ID','W'))
I expect an ouptut like this :
pfl adc
date 20030103
cur USD
ID W
KLP 0.41
TRF 0.59
pfl adc
date 20030104
cur USD
ID W
UTC 0.35
ZSE 0.65
解决方案
stack
是您正在寻找的:
df.stack().droplevel(0)
推荐阅读
- javascript - 即使链接不同,jQuery 和 CDN 也会显示为相同的图像
- php - PHP number_format not working as expected
- html - 我无法在 Visual Studio 上安装 Live Server 扩展,并且它不会出现在 VS 内的市场搜索中
- ruby-on-rails - 没有路由匹配 {:action=>"show", :controller=>"conversations", :recipient_id=>2, :sender_id=>1},缺少必需的键:[:id]
- path - symstore.exe 索引符号时抛出错误代码 122
- angular - 如何动态生成组件
- postgresql - bt_page_stats 中的 avg_item_size 是什么?
- javascript - 使用 svelte 或 react 在 cdn 上的三个.js
- r - 在 R 中绘制平面:PCA
- r - 比较 R 中的两列对