Prou Prou Tyu Prou Prou Tyu - 3 months ago 9
Python Question

How to name a dataframe column filled by numpy array?

I am filling a DataFrame by transposing some numpy array :

for symbol in syms[:5]:
price_p = Share(symbol)
closes_p = [c['Close'] for c in price_p.get_historical(startdate_s, enddate_s)]
dump = np.array(closes_p)
na_price_ar.append(dump)
print symbol
df = pd.DataFrame(na_price_ar).transpose()


df, the DataFrame is well filled however, the column name are 0,1,2...,5 I would like to rename them with the value of the element syms[:5]. I googled it and I found this:

for symbol in syms[:5]:
df.rename(columns={ ''+ str(i) + '' : symbol}, inplace=True)
i = i+1


But if I check the variabke df I still have the same column name.
Any ideas ?

Answer

Instead of using a list of arrays and transposing, you could build the DataFrame from a dict whose keys are symbols and whose values are arrays of column values:

import numpy as np
import pandas as pd
np.random.seed(2016)
syms = 'abcde'
na_price_ar = {}
for symbol in syms[:5]:
    # price_p = Share(symbol)
    # closes_p = [c['Close'] for c in price_p.get_historical(startdate_s, enddate_s)] 
    # dump = np.array(closes_p)
    dump = np.random.randint(10, size=3)
    na_price_ar[symbol] = dump
    print(symbol)

df = pd.DataFrame(na_price_ar)
print(df)

yields

   a  b  c  d  e
0  3  3  8  2  4
1  7  8  7  6  1
2  2  4  9  3  9
Comments