按列名排序数据帧按原样保留前3列

时间:2017-01-11 19:41:24

标签: python pandas dataframe multiple-conditions

我的数据框的列为

[u'Instructor Name', u'Product Name', u'Product Url', u' 2016.12.03',
u' 2016.12.05', u' 2016.12.01', u' 2016.12.12', u' 2016.12.16', 
u'2016.12.15', u' 2016.12.13', u' 2016.12.10', u' 2016.12.06', 
u'2016.12.14',u'2016.12.08', u' 2016.12.07', u' 2016.12.18', 
u'2016.12.11', u' 2016.12.04', u'2016.12.09', u' 2016.12.02', u' 2016.12.17']

我想对列进行排序,但仅限于'产品网址'。我想要输出

[u'Instructor Name', u'Product Name', u'Product Url',
       u' 2016.12.01', u' 2016.12.02', u' 2016.12.03', u' 2016.12.04',
       u' 2016.12.05', u' 2016.12.06', u' 2016.12.07', u' 2016.12.08',
       u' 2016.12.09', u' 2016.12.10', u' 2016.12.11', u' 2016.12.12',
       u' 2016.12.13', u' 2016.12.14', u' 2016.12.15', u' 2016.12.16',
       u' 2016.12.17', u' 2016.12.18', u' 2016.12.19']

怎么做。

1 个答案:

答案 0 :(得分:1)

我认为您需要投放% go run flagset.go init --filename=foo.json foo bar init foo.json [foo bar] % go run flagset.go check --filename=bar.json 1 2 3 check bar.json [1 2 3] % go run flagset.go run run % cat flagset.go package main import ( "flag" "fmt" "os" ) func main() { init := flag.NewFlagSet("init", flag.ExitOnError) initFile := init.String("filename", "myconfig.json", "configuration file") check := flag.NewFlagSet("check", flag.ExitOnError) checkFile := check.String("filename", "myconfig.json", "configuration file") if len(os.Args) <= 1 { flag.Usage() os.Exit(1) } switch os.Args[1] { case "init": if err := init.Parse(os.Args[2:]); err == nil { fmt.Println("init", *initFile, init.Args()) } case "check": if err := check.Parse(os.Args[2:]); err == nil { fmt.Println("check", *checkFile, check.Args()) } case "run": fmt.Println("run") } } to_datetimesort_values,如果需要dates则使用date

dates

样品:

df.columns = df.columns[:3].tolist() + 
            (pd.to_datetime(df.columns[3:], format='%Y.%m.%d')).sort_values().date.tolist()

如果需要相同格式,请添加strftime

cols = [u'Instructor Name', u'Product Name', u'Product Url', u' 2016.12.03', u' 2016.12.05', 
        u' 2016.12.01', u' 2016.12.12', u' 2016.12.16', u' 2016.12.15', u' 2016.12.13', 
        u' 2016.12.10', u' 2016.12.06', u'2016.12.14',u'2016.12.08', u' 2016.12.07', 
        u' 2016.12.18', u' 2016.12.11', u' 2016.12.04', u'2016.12.09',
 u' 2016.12.02', u' 2016.12.17']

df = pd.DataFrame(columns=cols) 
print (df)
Empty DataFrame
Columns: [Instructor Name, Product Name, Product Url,  
          2016.12.03,  2016.12.05,  2016.12.01,  2016.12.12,  2016.12.16,  2016.12.15,  
          2016.12.13,  2016.12.10,  2016.12.06, 2016.12.14, 2016.12.08,  2016.12.07,  
          2016.12.18,  2016.12.11,  2016.12.04, 2016.12.09,  2016.12.02,  2016.12.17]

Index: []

[0 rows x 21 columns]

df.columns = df.columns[:3].tolist() + 
             (pd.to_datetime(df.columns[3:], format='%Y.%m.%d')).sort_values().date.tolist()

print (df)
Empty DataFrame
Columns: [Instructor Name, Product Name, Product Url, 
          2016-12-01, 2016-12-02, 2016-12-03, 2016-12-04, 2016-12-05, 2016-12-06, 
          2016-12-07, 2016-12-08, 2016-12-09, 2016-12-10, 2016-12-11, 2016-12-12, 
          2016-12-13, 2016-12-14, 2016-12-15, 2016-12-16, 2016-12-17, 2016-12-18]
Index: []

[0 rows x 21 columns]