Note that there are some explanatory texts on larger screens.

plurals
  1. POReindexing and filling NaN values in Pandas
    primarykey
    data
    text
    <p>Consider this dataset: </p> <pre><code>data_dict = {'ind' : [1, 2, 3, 4], 'location' : [301, 301, 302, 303], 'ind_var' : [4, 8, 10, 15], 'loc_var' : [1, 1, 7, 3]} df = pd.DataFrame(data_dict) df_indexed = df.set_index(['ind', 'location']) df_indexed </code></pre> <p>which looks like</p> <pre><code> ind_var loc_var ind location 1 301 4 1 2 301 8 1 3 302 10 7 4 303 15 3 </code></pre> <p>ind_var is a variable that varies by ind ( = individual) and loc_var varies by location. (I also have an extra variable that varies by both ind and location, but I'm omitting it to simplify the presentation)</p> <p>I need to transform the data to have each individual index contain all the possible locations. I can reindex in this way (just showing individuals 1 to 3):</p> <pre><code>new_shape = [(1, 301), (1, 302), (1, 303), (2, 301), (2, 302), (2, 303), (3, 301), (3, 302), (3, 303)] idx = pd.Index(new_shape) df2 = df_indexed.reindex(idx, method = None) df2.index.names = ['id', 'location'] </code></pre> <p>which gives</p> <pre><code> ind_var loc_var id location 1 301 4 1 302 NaN NaN 303 NaN NaN 2 301 8 1 302 NaN NaN 303 NaN NaN 3 301 NaN NaN 302 10 7 303 NaN NaN </code></pre> <p>but I need a way to fill the missing values, so that I get:</p> <pre><code> ind_var loc_var id location 1 301 4 1 302 4 7 303 4 3 2 301 8 1 302 8 7 303 8 3 3 301 10 1 302 10 7 303 10 3 </code></pre> <p>I tried two different things with no success:</p> <p>1) Using a loc_dict = {301 : 1, 302 : 7, 303 : 3} to replace loc_var and a ind_dict = {1 : 4, 2: 8, 3: 10, 4 : 15} to replace ind_var</p> <p>2) Using a groupby method.</p> <pre><code># First reset index df_non_indexed = df2.reset_index() df_non_indexed['loc_var'] = df_non_indexed.groupby(['location'])['loc_var'].transform(lambda x: x.fillna(method='ffill')) </code></pre> <p>This almost works, but only does the fill forward (or backwards)</p> <p>There must be a very simple way of doing this, but I haven't been able to figure it out! Thanks for your time.</p> <p><em>Note</em>: this is related to my question <a href="https://stackoverflow.com/questions/17688155/complicated-for-me-reshaping-from-wide-to-long-in-pandas" title="reshaping from wide to long">reshaping from wide to long</a>. I've taken a different approach and simplified in hope that this one is easier to understand.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload