python 3.x - Filling in nans for numbers in a column-specific way -
given dataframe
, list
of indexes, there efficient pandas
function put nan
value values vertically preceeding each of entries of list?
for example, suppose have list [4,8]
, following dataframe
:
index 0 1 5 1 2 2 9 3 4 3.2 3 8 9 8.7
the desired output simply:
index 0 1 5 nan nan 2 nan nan 4 3.2 nan 8 9 8.7
any suggestions such function fast?
here's 1 numpy approach based on np.searchsorted
-
s = [4,8] = df.values idx = df.index.values sidx = np.argsort(idx) matching_row_indx = sidx[np.searchsorted(idx, s, sorter = sidx)] mask = np.arange(a.shape[0])[:,none] < matching_row_indx a[mask] = np.nan
sample run -
in [107]: df out[107]: 0 1 index 5 1.0 2.0 2 9.0 3.0 4 3.2 3.0 8 9.0 8.7 in [108]: s = [4,8] in [109]: = df.values ...: idx = df.index.values ...: sidx = np.argsort(idx) ...: matching_row_indx = sidx[np.searchsorted(idx, s, sorter = sidx)] ...: mask = np.arange(a.shape[0])[:,none] < matching_row_indx ...: a[mask] = np.nan ...: in [110]: df out[110]: 0 1 index 5 nan nan 2 nan nan 4 3.2 nan 8 9.0 8.7
Comments
Post a Comment