根据int类型对dataframe排序
合并数组,sort_values排序
train['a']=train['a'].astype(int)
train['b']=train['b'].astype(int)
train['c']=train['c'].astype(int)
train['abc']=train['a']*1000+train['b']+train['c']/10000
train=train.sort_values(by="abc" , ascending=True)
对多列,sort_values排序
def sort_by_id(df):
"""把文本顺序严格按id排列"""
id_df = pd.DataFrame(df['id'].apply(lambda x: x.split("_")).tolist())
id_df.columns = ['drama', 'scene', 'tag', 'number']
id_df.drop(['tag'], inplace=True, axis=1)
id_df = id_df.astype("int")
df = pd.concat([df, id_df], axis=1)
df = df.sort_values(by=['drama', 'scene', 'number'])
# 后面有用先不删
# df.drop(['drama', 'scene', 'number'], inplace=True, axis=1)
return df