Column is not appended to pandas DataFrame

110 views Asked by At

I want to add subtotals to my dataframe: groupby some index level then append new dataframe to the main one. For some unknown reason temp["Узел"] = "Итого" does nothing. It doesn't add new column, though temp["Узел2"] = "Итого" adds one. I think, it's because my 'pvt' dataframe already has "Узел" index level, but what impact does it have on a new 'temp' dataframe?

temp = pvt.groupby(level=["Принадл"]).sum()
temp["Узел"] = "Итого"
print(temp)
print(temp["Узел"])
        Россия                                                                 \
           ОКТ   КЛГ    МСК    ГОР    СЕВ    СКВ    ЮВС    ПРВ    КБШ     СВР   
Принадл                                                                         
ИП         783    14    172    398    248   1178    460    235    314     644   
ПС       93900  5049  89815  36197  85619  55213  91681  26764  33869  154280   

          ...                                                            \
        СНГ и др.    Итого  

Принадл                     
ИП             46     9342  
ПС          51529  1299784  

[2 rows x 21 columns]

Empty DataFrame
Columns: []
Index: [ИП, ПС]

pandas 0.16.1, numpy 1.9.2

UPD: that's because of manually added multiindex level "Узел" or multilevel columns... or both. I'm not sure yet. UPD2: i've been able to avoid the problem when i temporary switched to one-level column names before adding new columns

columns, temp.columns = temp.columns, [None]*len(temp.columns)
temp[...] = ...
<...>
temp.columns = columns
0

There are 0 answers