问题标签用户未答复将特定列移到数据框中特定列的之前/之后

时间:2019-03-18 07:03:58

标签: python pandas dataframe data-cleaning

在数据框示例中:

<-- this one succeeds!

'medcine_preg_oth1''medcine_preg_oth2''medcine_preg_oth3',这三列与其他列在数据框中。 。我想将这三个:medcine_preg_oth1 medcine_preg_oth2 medcine_preg_oth3移到“ medcine_preg_oth”之后的位置。

我的想法是将特定列移到数据框中特定列的后面/之前,以实现更广泛的用途。请建议我!谢谢

1 个答案:

答案 0 :(得分:0)

您可以像这样重新排列您的列:

re_ordered_columns = ['medicine_pred_oth','medcine_preg_oth1','medcine_preg_oth2','medcine_preg_oth3']
df = df[re_ordered_columns+df.columns.difference(re_ordered_columns).tolist()]

添加剩余的列代替...