Skip to content
Advertisement

Shift column position to right based on criteria using Pandas

I have a dataframe that looks like below

df = pd.DataFrame({'person_id': [101,101,101,101,202,202],
                   'person_type':['A','A','B','C','D','B'],
                   'course_id':[121,123,135,137,140,143],
                   'dep_id':[np.nan,Z12,np.nan,B12,np.nan,D12})

I would like to position shift by 1 cell to the right if there is NA in the column dep_id.

I tried the below but it wasn’t working

l =  list(np.arange(0,len(df.columns) -1))
np.where(df['dep_id'].isna(), l.insert(0,-1))
df.iloc[:,l]

Any efficient and elegant approach to shift column position on big data?

For example, I expect my output to be like as shown below

enter image description here

Advertisement

Answer

Let us try shift along axis=1

m = df['dep_id'].isna()
df.loc[m, :] = df[m].shift(axis=1)

   person_id person_type course_id dep_id
0        NaN         101         A    121
1      101.0           A       123    Z12
2        NaN         101         B    135
3      101.0           C       137    B12
4        NaN         202         D    140
5      202.0           B       143    D12
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement