python - 在 Pandas 中重新索引和填充 NaN 值

标签 python pandas reshape

考虑这个数据集:

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

看起来像

           ind_var loc_var
ind location        
1   301      4      1
2   301      8      1
3   302     10      7
4   303     15      3

ind_var 是随 ind(= 个体)而变化的变量,loc_var 随位置而变化。 (我还有一个额外的变量,它随 ind 和位置而变化,但我省略了它以简化演示)

我需要转换数据以使每个单独的索引包含所有可能的位置。 我可以用这种方式重建索引(只显示个人 1 到 3):

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']

给出

        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

但我需要一种方法来填充缺失值,以便我得到:

        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

我尝试了两种不同的方法但没有成功:

1) 使用 loc_dict = {301 : 1, 302 : 7, 303 : 3} 替换 loc_var 并使用 ind_dict = {1 : 4, 2: 8, 3: 10, 4 : 15} 替换 ind_var

2) 使用groupby方法。

# 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')) 

这几乎可以工作,但只能向前(或向后)填充

肯定有一种非常简单的方法可以做到这一点,但我一直没弄清楚! 感谢您的宝贵时间。

注意:这与我的问题有关 reshaping from wide to long .我采用了不同的方法并进行了简化,希望这个方法更容易理解。

最佳答案

这可以通过 stack/unstackgroupby 很容易地完成:

# unstack to wide, fillna as 0s
df_wide = df_indexed.unstack().fillna(0)
# stack back to long
df_long = df_wide.stack()
# change 0s to max using groupby.
df_long['ind_var'] = df_long['ind_var'].groupby(level = 0).transform(lambda x: x.max())
df_long['loc_var'] = df_long['loc_var'].groupby(level = 1).transform(lambda x: x.max())
print df_long

结果如下:

                   ind_var  loc_var
ind 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
4   301            15        1
    302            15        7
    303            15        3

关于python - 在 Pandas 中重新索引和填充 NaN 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17730252/

相关文章:

python - 使用 python requests 工具带捕获文件名

python - Numpy 2D Array - Power Of - 不返回答案?

python - json 到 DataFrame 转换问题

python - 切片数组返回奇怪的形状

r - 从 group1、group2、overlap_count 创建重叠矩阵?

r - 对于数据框中的多列,在 R 中从宽变长

python - Selenium Firefox webdriver 适用于从 Ubuntu 构建的图像,但不适用于从 Debian 构建的图像

python - 在 PostgreSQL 中创建存储过程时出错

python - 网址错误 : urlopen error [Errno 2] No such file or directory

python - 在 Pandas 数据帧上使用 ttest_ind 时遇到问题