0

需要重新组装一个数据帧,该数据帧是分组操作的结果。假定已订购。

   Major  Minor  RelType  SomeNulls
0    0.0    0.0        1       1.0
1    NaN    NaN        2       NaN
2    1.0    1.0        1       NaN
3    NaN    NaN        2       NaN
4    NaN    NaN        3       NaN
5    2.0    3.0        1       NaN
6    NaN    NaN        2       2.0

并寻找这样的东西

Major  Minor  RelType  SomeNulls
0    0.0    0.0        1       1.0
1    0.0    0.0        2       NaN
2    1.0    1.0        1       NaN
3    1.0    1.0        2       NaN
4    1.0    1.0        3       NaN
5    2.0    3.0        1       NaN
6    2.0    3.0        2       2.0

想知道是否有一种优雅的方法来解决它。

import pandas as pd
import numpy as np

def refill_frame(df, cols):
    while df[cols].isnull().values.any():
        for col in cols:
            if col in list(df):
            #print (col)
                df[col]= np.where(df[col].isnull(), df[col].shift(1), df[col])
    return df

df = pd.DataFrame({'Major': [0, None, 1, None, None,2, None], 
                   'Minor': [0, None, 1, None, None,3, None],
                   'RelType': [1, 2, 1, 2,3, 1,2],
                   'SomeNulls': [1, None,None, None,None,None,2] 
                  })
print (df)

cols2fill =['Major', 'Minor']

df = refill_frame(df, cols2fill) 
print (df)  
4

1 回答 1

1

如果我正确理解了这个问题,您可以对特定列进行转换:

df.loc[:, ['Major', 'Minor']] = df.loc[:, ['Major', 'Minor']].transform('ffill')


   Major  Minor  RelType  SomeNulls
0    0.0    0.0        1        1.0
1    0.0    0.0        2        NaN
2    1.0    1.0        1        NaN
3    1.0    1.0        2        NaN
4    1.0    1.0        3        NaN
5    2.0    3.0        1        NaN
6    2.0    3.0        2        2.0

您还可以使用pyjanitor的fill_direction函数:

# pip install pyjanitor
import janitor

df.fill_direction({"Major":"down", "Minor":"down"})

   Major  Minor  RelType  SomeNulls
0    0.0    0.0        1        1.0
1    0.0    0.0        2        NaN
2    1.0    1.0        1        NaN
3    1.0    1.0        2        NaN
4    1.0    1.0        3        NaN
5    2.0    3.0        1        NaN
6    2.0    3.0        2        2.0
于 2021-03-06T13:43:34.693 回答