首页 > 解决方案 > 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

标签: pythonpandas

解决方案


stack是您正在寻找的:

df.stack().droplevel(0)

推荐阅读