Pandas. A pretty way to delete cell and shift left others in row?

核能气质少年 提交于 2021-02-11 06:51:44

问题


In dataframe I need to delete some cells and shift left others in row :

df=pd.DataFrame({'X0':['anytext','anytext','anytext','anytext','anytext'],
                 'X1':['12:40','boss','engen','15:44','16:01'],
                 'X2':['anytext','12:44','14:06','anytext','anytext'],
                 'X3':['anytext','anytext','anytext','anytext','anytext']})

 df
  
        X0     X1       X2       X3
0  anytext  12:40  anytext  anytext
1  anytext   boss    12:44  anytext
2  anytext  engen    14:06  anytext
3  anytext  15:44  anytext  anytext
4  anytext  16:01  anytext  anytext

I want to delete "boss" and "engen" and shift left other cells in row:

        X0     X1       X2       X3
0  anytext  12:40  anytext  anytext
1  anytext  12:44  anytext      NaN
2  anytext  14:06  anytext      NaN
3  anytext  15:44  anytext  anytext
4  anytext  16:01  anytext  anytext

回答1:


You need select rows for shifting, e.g. here is tested if first 2 values in X1 are numeric by str[:2] and Series.str.isnumeric, invert mask by ~, so only for non numeric value use DataFrame.shift:

m = ~df['X1'].str[:2].str.isnumeric()

Another idea for mask, thank you @Manakin is test if datetimes in format HH:MM:

m = pd.to_datetime(df['X1'],format='%H:%M',errors='coerce').isna()

Also if want test numeric 2 numbers with : with length 2:

m = ~df['X1'].str.contains('^\d{2}:\d{2}$')

df[m] = df[m].shift(-1, axis=1)
print(df)
      X1       X2       X3
0  12:40  anytext  anytext
1  12:44  anytext      NaN
2  14:06  anytext      NaN
3  15:44  anytext  anytext
4  16:01  anytext  anytext

If need modify all columns after X1 one idea:

df=pd.DataFrame({'X0':['anytext','anytext','anytext','anytext','anytext'],
                 'X1':['12:40','boss','engen','15:44','16:01'],
                 'X2':['anytext','12:44','14:06','anytext','anytext'],
                 'X3':['anytext','anytext','anytext','anytext','anytext']}) 

m = ~df['X1'].str.contains('^\d{2}:\d{2}$')
df.loc[m, 'X1':] =df.loc[m, 'X1':].shift(-1, axis=1)
print(df)
       X0     X1       X2       X3
0  anytext  12:40  anytext  anytext
1  anytext  12:44  anytext      NaN
2  anytext  14:06  anytext      NaN
3  anytext  15:44  anytext  anytext
4  anytext  16:01  anytext  anytext

Another with convert X0 to index:

df = df.set_index('X0')
m = ~df['X1'].str.contains('^\d{2}:\d{2}$')
df[m] = df[m].shift(-1, axis=1)
df = df.reset_index()
print(df)
        X0     X1       X2       X3
0  anytext  12:40  anytext  anytext
1  anytext  12:44  anytext      NaN
2  anytext  14:06  anytext      NaN
3  anytext  15:44  anytext  anytext
4  anytext  16:01  anytext  anytext


来源:https://stackoverflow.com/questions/66027375/pandas-a-pretty-way-to-delete-cell-and-shift-left-others-in-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!