Pandas切片和索引与fillna一起使用

时间:2016-08-23 07:15:35

标签: python pandas

我有一个pandas数据框 tdf 我正在基于布尔标签提取切片

idx = tdf['MYcol1'] == 1
myslice = tdf.loc[idx] //I want myslice to be a view not a copy

现在我想填写 myslice 列中的缺失值,我希望这能反映在 tdf 我的原始数据框

myslice.loc[:,'MYcol2'].fillna(myslice['MYcol2'].mean(), inplace = True) // 1
myslice.ix[:,'MYcol2'].fillna(myslice['MYcol2'].mean(), inplace = True) // 2

上面的1和2都抛出警告:正在尝试在DataFrame的切片副本上设置一个值

我做错了什么?

1 个答案:

答案 0 :(得分:2)

将其分配给新变量时,会创建副本。你之后做的事情是无关紧要的。考虑一下:

myslice.is_copy
Out: <weakref at 0x000001CC842FD318; to 'DataFrame' at 0x000001CC8422D6A0>

填写NaN&#39;在myslice中:

tdf.loc[:, 'B'].fillna(tdf.loc[idx, 'B'].mean(), inplace=True)

tdf
Out: 
          A         B         C
0       NaN  0.195070 -1.781563
1 -0.729045  0.196557  0.354758
2  0.616887  0.008628       NaN
3       NaN  0.008628  0.037006
4  0.767902  0.008628       NaN
5 -0.805627  0.008628       NaN
6  1.133080  0.008628 -0.659892
7 -1.139802  0.784958 -0.554310
8 -0.470638 -0.216950       NaN
9 -0.392389 -3.046143  0.543312

它没有反映在tdf中,因为:

myslice['B'].mean()

如果您将其更改为:

<div align="center" class="title">Statistiche:</div>
<div id="outer_container">
<div id="container">
<span class='multislider'>Hokuto ShinKen (<input type='text' required name='question_3' class='amount' value='5' data-min='0' data-max='100'/>)
<div class='slider tied'>
</div>
Nanto (<input type='text' required name='question_3' class='amount' value='15' data-min='0' data-max='100'/>)
<div class='slider tied'>
</div>
Gento (<input type='text' required name='question_3' class='amount' value='50' data-min='0' data-max='100'/>)
<div class='slider tied'>
</div>
Artiglio del Monte Taishan (<input type='text' required name='question_3' class='amount' value='30' data-min='0' data-max='100'/>)
<div class='slider tied'></div>
</span>
</div>
</div>

然后它的工作原理。在最后一部分中,您还可以使用 var init = true; var elements = $(".multislider").children(".slider.tied").length; var MAX = 100; var inpVal = $('.txt_name').val(); var initValue = (inpVal) >> 0; //var initValue = (MAX / elements) >> 0; var InitMod = MAX % elements; $(".slider.tied").each(function() { var slidersTied = $(".slider.tied"); var context = $(this); var input = context.prev(".amount"); var val = input.data('answer'); var min = input.data('min'); var max = input.data('max'); var range = 1; var proceed = false; $(this).empty().slider({ value: val, min: min, max: max, range: range, animate: "slow", create: function(event, ui){ if (InitMod > 0) { $(this).slider('value', initValue + 1); $(this).prev('.amount').val(initValue + 1); InitMod = InitMod - 1; } else { $(this).slider('value', initValue); $(this).prev('.amount').val(initValue); } }, slide: function(e, ui) { // Update display to current value $(this).prev('.amount').val(ui.value); var current = ($(this).index() / 2) >> 0; var total = 0; var counter = 0 slidersTied.not(this).each(function() { total += $(this).slider("option", "value"); counter += 1; }); total += ui.value; if (total != MAX){ proceed = true; } var missing = MAX - total; console.log("missing: " + missing); counter = 0; if(proceed) { //carico vettore elementi var elements = []; slidersTied.each(function() { elements[counter] = $(this); counter += 1; }); var endIndex = counter - 1; counter = endIndex + 1; while (missing != 0) { console.log("current counter: " + counter); do { if (counter == 0) { counter = endIndex; } else { counter = counter - 1; } } while(counter == current) console.log("elemento attuale: " + counter); var value = elements[counter].slider("option", "value"); var result = value + missing; if (result >= 0) { elements[counter].slider('value', result); elements[counter].prev('.amount').val(result); missing = 0; } else { missing = result; elements[counter].slider('value', 0); elements[counter].prev('.amount').val(0); } } } } }); }); ,因为您没有更新这些值。但左侧应该是原始的DataFrame。