<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: conditionally use pandas df.fillna(method=bfill) in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/conditionally-use-pandas-df-fillna-method-bfill/m-p/1158621#M64185</link>
    <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/363906"&gt;@jcarlson&lt;/a&gt;,&amp;nbsp;Great!&amp;nbsp; Thank you.&amp;nbsp; I had danced around the loc filter, but lost focus.&amp;nbsp; I touched it up and applied it back to the full df and ended up with this...'the desired output'.&amp;nbsp; Cheers.&amp;nbsp; Tyler&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TylerT_1-1648506039491.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/37519i14747D761FA66D7D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="TylerT_1-1648506039491.png" alt="TylerT_1-1648506039491.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 28 Mar 2022 22:20:49 GMT</pubDate>
    <dc:creator>TylerT</dc:creator>
    <dc:date>2022-03-28T22:20:49Z</dc:date>
    <item>
      <title>conditionally use pandas df.fillna(method=bfill)</title>
      <link>https://community.esri.com/t5/python-questions/conditionally-use-pandas-df-fillna-method-bfill/m-p/1158573#M64181</link>
      <description>&lt;P&gt;Forward fill and back fill are handy, but I'm struggling to apply it conditionally or to a specific filtered area of my data frame.&amp;nbsp; See the follow snip for the input and desired output.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TylerT_0-1648499216946.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/37502i5E72A65F40688725/image-size/medium?v=v2&amp;amp;px=400" role="button" title="TylerT_0-1648499216946.png" alt="TylerT_0-1648499216946.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Notice, I am focusing in on ONLY columns unit:cid and rows with 8000 (salmon region).&amp;nbsp; Also, notice how item b969 does not backfill nor do any 9000 or 7000 rows.&amp;nbsp; &amp;nbsp;The red numbers are the desired new values.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tinkered around with filters, np.where, setting inplace=True, and of course axis=1...all without luck.&amp;nbsp; I end up with errors or unexpected results.&lt;/P&gt;&lt;P&gt;Following are the input data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;hoc	item	category	unit	fid	cid
bbca	a954	9000	NaN	NaN	400
ccd	b858	9000	NaN	NaN	NaN
aba	b868b	8000	NaN	NaN	350
NaN	b969	8000	NaN	7	NaN
aba	a55	7000	NaN	NaN	450
ccd	c1	7000	NaN	8	NaN&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Copy the data above to your clipboard and use pd.read_clipboard() and you should be off and running with the input dataframe.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TylerT_0-1648499628930.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/37505iC9C4ACB18DAFABB3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="TylerT_0-1648499628930.png" alt="TylerT_0-1648499628930.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Much Appreciated.&lt;/P&gt;&lt;P&gt;Tyler&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Mar 2022 20:40:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/conditionally-use-pandas-df-fillna-method-bfill/m-p/1158573#M64181</guid>
      <dc:creator>TylerT</dc:creator>
      <dc:date>2022-03-28T20:40:05Z</dc:date>
    </item>
    <item>
      <title>Re: conditionally use pandas df.fillna(method=bfill)</title>
      <link>https://community.esri.com/t5/python-questions/conditionally-use-pandas-df-fillna-method-bfill/m-p/1158603#M64182</link>
      <description>&lt;P&gt;For applying to a specific &lt;EM&gt;known &lt;/EM&gt;area, the &lt;STRONG&gt;loc&lt;/STRONG&gt; method is probably the best.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;df.loc[2:3,['unit', 'fid', 'cid']].fillna(method='bfill', axis=1)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To apply this conditionally, simply provide a boolean test for the rows. For instance, here's me backfilling all of the rows that are &lt;EM&gt;not &lt;/EM&gt;category 8000:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;df.loc[df.category != 8000, ['unit', 'fid', 'cid']].fillna(method='bfill', axis=1)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jcarlson_0-1648503777053.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/37515iC068BC6F112E2C57/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jcarlson_0-1648503777053.png" alt="jcarlson_0-1648503777053.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Mar 2022 21:42:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/conditionally-use-pandas-df-fillna-method-bfill/m-p/1158603#M64182</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2022-03-28T21:42:48Z</dc:date>
    </item>
    <item>
      <title>Re: conditionally use pandas df.fillna(method=bfill)</title>
      <link>https://community.esri.com/t5/python-questions/conditionally-use-pandas-df-fillna-method-bfill/m-p/1158621#M64185</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/363906"&gt;@jcarlson&lt;/a&gt;,&amp;nbsp;Great!&amp;nbsp; Thank you.&amp;nbsp; I had danced around the loc filter, but lost focus.&amp;nbsp; I touched it up and applied it back to the full df and ended up with this...'the desired output'.&amp;nbsp; Cheers.&amp;nbsp; Tyler&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TylerT_1-1648506039491.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/37519i14747D761FA66D7D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="TylerT_1-1648506039491.png" alt="TylerT_1-1648506039491.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Mar 2022 22:20:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/conditionally-use-pandas-df-fillna-method-bfill/m-p/1158621#M64185</guid>
      <dc:creator>TylerT</dc:creator>
      <dc:date>2022-03-28T22:20:49Z</dc:date>
    </item>
  </channel>
</rss>

