Python Pandas:转换多索引数据帧

时间:2014-06-03 00:52:45

标签: python numpy pandas

我将此数据集作为带有multiIndex的pandas DataFrame:

                       cnt                                    
loginsmonth     2014-02-01  2014-03-01  2014-04-01  2014-05-01
app regmonth                                                  
1   2014-02-01        6069        1837         107          54
    2014-03-01           0       10742        2709        1387
    2014-04-01           0           0        5584        1103
    2014-05-01           0           0           0        5584

我需要将其转换为与对角线相关的百分比值:

                       cnt                                    
loginsmonth     2014-02-01  2014-03-01  2014-04-01  2014-05-01
app regmonth                                                  
1   2014-02-01   6069/6069   1837/6069    107/6069     54/6069
    2014-03-01           0 10742/10742  2709/10742  1387/10742
    2014-04-01           0           0   5584/5584   1103/5584
    2014-05-01           0           0           0   5584/5584

1 个答案:

答案 0 :(得分:1)

如果你不介意转换对角线,你可以这样做:

#create dataset
data = pd.DataFrame({'2014-02-01': [6069,0,0,0], '2014-03-01': [1837,1042,0,0], '2014-04-01': [107,209,5584,0], '2014-05-01': [54,1387,1103,5384]}, index = [[1,1,1,1], ['2014-02-01', '2014-03-01', '2014-04-01', '2014-05-01']], columns = ['2014-02-01', '2014-03-01', '2014-04-01', '2014-05-01'])

#transpose dataset
data = data.T

#compute percentages
for x, col in enumerate(data):
    data[col] = [item/data[col][x] for item in data[col]]

#you can always re transpose back!
data = data.T