Space
Space

Reputation: 1040

How to re-order dataframe fields?

I have the table below contained in the DataFrame below :

timestamp   val1    val2    user_id  val3  val4    val5    val6
01/01/2011  1   100 3    5     100     3       5
01/02/2013  20  8        6     12      15      3
01/07/2012      19  57   10    9       6       6        
01/11/2014  3100    49  6        12    15      3
21/12/2012          240  30    240     30       
14/09/2013      21  63                  
01/12/2013  3200    51  20       50

I would like to know how I can obtain another dataframe where fields/columns will be re-ordered this way: timestamp, user_id, val1, val2, val3.

The code used in order to obtain the above table is :

import pandas as pd

newnames = ['timestamp', 'val1', 'val1','val2', 'val3','user_id']
df = pd.read_csv('mytest.csv', names = newnames, header = False, parse_dates=True, dayfirst=True)
df['timestamp'] = pd.to_datetime(df['timestamp'], dayfirst=True) 

Thanks in advance for your help.

Upvotes: 1

Views: 129

Answers (1)

astrel-
astrel-

Reputation: 64

Try this one

df = df.loc[:,['timestamp', 'user_id', 'val1', 'val2', 'val3']]

Upvotes: 4

Related Questions