Moving a specific column of a dataframe to first column

Question:

I have a dataframe as follows:

s = df.head().to_dict()
print(s)

{'BoP transfers': {1998: 12.346282212735618,
  1999: 19.06438060024298,
  2000: 18.24888031473687,
  2001: 24.860019912667006,
  2002: 32.38242225822908},
 'Current balance': {1998: -6.7953,
  1999: -2.9895,
  2000: -3.9694,
  2001: 1.1716,
  2002: 5.7433},
 'Domestic demand': {1998: 106.8610389799729,
  1999: 104.70302507466538,
  2000: 104.59254229534136,
  2001: 103.83532232336977,
  2002: 102.81709401489702},
 'Effective exchange rate': {1998: 88.134,
  1999: 95.6425,
  2000: 99.927725,
  2001: 101.92745,
  2002: 107.85565},
 'RoR (foreign liabilities)': {1998: 0.0433,
  1999: 0.0437,
  2000: 0.0542,
  2001: 0.0539,
  2002: 0.0474},
 'Gross foreign assets': {1998: 19.720897432405103,
  1999: 22.66200738564236,
  2000: 25.18270679890144,
  2001: 30.394226651732836,
  2002: 37.26477320359688},
 'Gross domestic income': {1998: 104.9037939043707,
  1999: 103.15361867816479,
  2000: 103.06777792080423,
  2001: 102.85886528974339,
  2002: 102.28518242008846},
 'Gross foreign liabilities': {1998: 60.59784839338306,
  1999: 61.03308220978983,
  2000: 64.01438055825233,
  2001: 67.07798172469921,
  2002: 70.16108592109364},
 'Inflation rate': {1998: 52.6613,
  1999: 19.3349,
  2000: 16.0798,
  2001: 15.076,
  2002: 17.236},
 'Credit': {1998: 0.20269913592846378,
  1999: 0.2154280880177353,
  2000: 0.282948948505006,
  2001: 0.3954812893893278,
  2002: 0.3578263032373988}}

which can be converted back to its original form using:

df = pd.DataFrame.from_dict(s)

Suppose, I want to move the column named "Gross foreign liabilities" to the first column. I know this can be done by reindexing. However, in my case the dataframe has 100 columns. How can I move say a specific column the very beginning? I read about pandas pop() method, but in my framework I get an error.

Asked By: Carl

||

Answers:

Here are a few ways I do it:

columns = df.columns.tolist()
columns.insert(0, columns.pop(columns.index("Gross foreign liabilities")))
df = df.reindex(columns=columns)

OR

col = ["Gross foreign liabilities"]
df = df[col + [x for x in df.columns if x not in col]]
Answered By: Jason Baker

You can use pop and insert:

name = 'Gross foreign liabilities'
df.insert(0, name, df.pop(name))

as a function:

def move_first(df, name):
    df.insert(0, name, df.pop(name))

move_first(df, 'Gross foreign liabilities')

Output:

      Gross foreign liabilities  BoP transfers  Current balance  
1998                  60.597848      12.346282          -6.7953   
1999                  61.033082      19.064381          -2.9895   
2000                  64.014381      18.248880          -3.9694   
2001                  67.077982      24.860020           1.1716   
2002                  70.161086      32.382422           5.7433   

      Domestic demand  Effective exchange rate  RoR (foreign liabilities)  
1998       106.861039                88.134000                     0.0433   
1999       104.703025                95.642500                     0.0437   
2000       104.592542                99.927725                     0.0542   
2001       103.835322               101.927450                     0.0539   
2002       102.817094               107.855650                     0.0474   

      Gross foreign assets  Gross domestic income  Inflation rate    Credit  
1998             19.720897             104.903794         52.6613  0.202699  
1999             22.662007             103.153619         19.3349  0.215428  
2000             25.182707             103.067778         16.0798  0.282949  
2001             30.394227             102.858865         15.0760  0.395481  
2002             37.264773             102.285182         17.2360  0.357826  
Answered By: mozway

Example

data = {'col1': {0: 0, 1: 2}, 'col2': {0: 1, 1: 3}, 'col3': {0: 2, 1: 4}}
df = pd.DataFrame(data)

df

    col1    col2    col3
0   0       1       2
1   2       3       4

Code

df.insert(0, 'col3', df.pop('col3'))

output(df):

    col3    col1    col2
0   2       0       1
1   4       2       3

make example more simple plz

Answered By: Panda Kim
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.