2

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 ?

0

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

Comments

0

You can use:

na_price_ar = [['A','B','C'],[0,2,3],[1,2,4],[5,2,3],[8,2,3]]

syms = ['q','w','e','r','t','y','u']
df = pd.DataFrame(na_price_ar, index=syms[:5]).transpose()
print (df)
   q  w  e  r  t
0  A  0  1  5  8
1  B  2  2  2  2
2  C  3  4  3  3

Comments

0

You may use as dictionary key into the .rename() method the df.columns[ number ] statement

dic = {'a': [4, 1, 3, 1], 'b': [4, 2, 1, 4], 'c': [5, 7, 9, 1], 'd': [4, 1, 3, 1], 'e': [5, 2, 6, 0]}
df = pd.DataFrame(dic)
number = 0
for symbol in syms[:5]:
    df.rename( columns = { df.columns[number]: symbol}, implace = True)
    number = number + 1

and the result is

   i  f  g  h  i
0  4  4  5  4  5
1  1  2  7  1  2
2  3  1  9  3  6
3  1  4  1  1  0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.