Ledger Yu Ledger Yu - 1 month ago 5
Python Question

Partially Renaming Columns in Pandas DataFrame

I'm trying to rename the first N columns of a DataFrame.

import pandas as pd
Dat = pd.read_csv(inputName, delimiter='\t', header=0)


The original table looks like this:

$Date $ciq_ticker $industry price ...
'09/30/2016 'AAPL' 'Technology' 100.00
'09/30/2016 'AMZN' 'Consumer' 1000.00
...


I want to make some column names more intuitive. What's in my mind is something like this:

descriptors = ['date','ticker','industry']
Dat.columns[:len(descriptors)] = descriptors


This gives an error of "Index does not support mutable operations".

I know something like this works:

Dat.rename(columns={'$Date': 'date', '$ciq_ticker': 'ticker', '$industry': 'industry'}, inplace=True)


But I just don't like the idea of having to type the original column names explicitly. Truth is, the real table has more than 20 columns that I need to modify.

Answer

Try this:

In [91]: cols = ['date','ticker','industry']

In [92]: df
Out[92]:
        $Date $ciq_ticker   $industry   price
0  09/30/2016        AAPL  Technology   100.0
1  09/30/2016        AMZN    Consumer  1000.0

In [93]: df.columns = cols + df.columns.tolist()[len(cols):]

In [94]: df
Out[94]:
         date ticker    industry   price
0  09/30/2016   AAPL  Technology   100.0
1  09/30/2016   AMZN    Consumer  1000.0
Comments