set_index equivalent for columns headings

The best way to handle this is to avoid getting into this situation.

How was df created? For example, if you used read_csv or a variant, then header=0 will tell read_csv to parse the first line as the column names.


Given df as you have it, I don't think there is an easier way to fix it than what you've described. To remove the first row, you could use df.iloc:

df = df.iloc[1:]

I'm not sure if this is more efficient, but you could try creating a data frame with the corect index and default column names out of your problem data frame, and then rename the columns also using the promlematic data frame. For example:

import pandas as pd
import numpy as np
from pandas import DataFrame

data = {'0':[' ', 'Jan', 'Feb', 'Mar', 'April'], \
        '1' : ['2013', 3926, 3456, 3245, 1254],  \
        '2' : ['2012', 3346, 4342, 1214, 4522],  \
        '3' : ['2011', 3946, 4323, 1214, 8922]}

DF = DataFrame(data)
DF2 = (DataFrame(DF.ix[1:, 1:]).set_index(DF.ix[1:,0]))
DF2.columns = DF.ix[0, 1:]
DF2

Tags:

Python

Pandas