Python:如何从Pandas数据框中选择索引?

我有一个数据框,如下所示:

df
     0    1   2  3   4      
0   0.0 NaN NaN NaN NaN 
1   NaN 0.0 0.0 NaN 4.0 
2   NaN 2.0 0.0 NaN 5.0
3   NaN NaN NaN 0.0 NaN 
4   NaN 0.0 3.0 NaN 0.0

I would like to have a dataframe of all the couples of values different from NaN. The dataframe should be like the following

df
    i   j   val
0   0   0   0.0 
1   1   1   0.0
2   1   2   0.0
3   1   4   5.0
4   3   3   0.0
5   4   1   0.0
6   4   2   3.0
7   4   4   0.0
评论
  • ksequi
    ksequi 回复

    Use DataFrame.stack with DataFrame.rename_axis and DataFrame.reset_index:

    df = df.stack().rename_axis(('i','j')).reset_index(name='val')
    print (df)
        i  j  val
    0   0  0  0.0
    1   1  1  0.0
    2   1  2  0.0
    3   1  4  4.0
    4   2  1  2.0
    5   2  2  0.0
    6   2  4  5.0
    7   3  3  0.0
    8   4  1  0.0
    9   4  2  3.0
    10  4  4  0.0
    
  • 谎言可笑
    谎言可笑 回复

    像这样:

    In [379]: df.stack().reset_index(name='val').rename(columns={'level_0':'i', 'level_1':'j'})                                                                                                                 
    Out[379]: 
        i  j  val
    0   0  0  0.0
    1   1  1  0.0
    2   1  2  0.0
    3   1  4  4.0
    4   2  1  2.0
    5   2  2  0.0
    6   2  4  5.0
    7   3  3  0.0
    8   4  1  0.0
    9   4  2  3.0
    10  4  4  0.0