Lengthening a DataFrame based on stacking columns within it in Pandas

后端 未结 3 1685
走了就别回头了
走了就别回头了 2021-01-23 04:30

I am looking for a function that achieves the following. It is best shown in an example. Consider:

pd.DataFrame([ [1, 2, 3 ], [4, 5, np.nan ]], columns=[\'x\', \         


        
3条回答
  •  星月不相逢
    2021-01-23 05:18

    Repeat all the items in first column based on counts of not null values in each row. Then simply create your final dataframe using the rest of not null values in other columns. You can use DataFrame.count() method to count not null values and numpy.repeat() to repeat an array based on a respective count array.

    >>> rest = df.loc[:,'y1':]
    >>> pd.DataFrame({'x': np.repeat(df['x'], rest.count(1)).values,
                      'y': rest.values[rest.notna()]})
    

    Demo:

    >>> df
        x   y1   y2   y3   y4
    0   1  2.0  3.0  NaN  6.0
    1   4  5.0  NaN  9.0  3.0
    2  10  NaN  NaN  NaN  NaN
    3   9  NaN  NaN  6.0  NaN
    4   7  6.0  NaN  NaN  NaN
    
    >>> rest = df.loc[:,'y1':]
    >>> pd.DataFrame({'x': np.repeat(df['x'], rest.count(1)).values,
                      'y': rest.values[rest.notna()]})
       x    y
    0  1  2.0
    1  1  3.0
    2  1  6.0
    3  4  5.0
    4  4  9.0
    5  4  3.0
    6  9  6.0
    7  7  6.0
    

提交回复
热议问题