David Lerech
David Lerech

Reputation: 55

How to pass variable as a column name pandas

I'm using Python 2.7 I try do create new column based on variable form a list

tickers=['BAC','JPM','WFC','C','MS'] returns=pd.DataFrame for tick in tickers: returns[tick]=bank_stocks[tick][[]1'Close'].pct_change()**

But I get this error

TypeError Traceback (most recent call last) in () 2 returns=pd.DataFrame 3 for tick in tickers: ----> 4 returns[tick]=bank_stocks[tick]['Close'].pct_change() 5

TypeError: 'type' object does not support item assignment

Upvotes: 2

Views: 4435

Answers (2)

Prady
Prady

Reputation: 11

Your code is correct except the line in In[73] where you must call dataframe(i.e., pd.DataFrame()) you have created an object by not using '()' after DataFrame. Thats why the error is type object doesnot support assignment.

Upvotes: 1

jezrael
jezrael

Reputation: 862481

IIUC you need:

np.random.seed(100)
mux = pd.MultiIndex.from_product([['BAC','JPM','WFC','C','MS', 'Other'], ['Close', 'Open']])
df = pd.DataFrame(np.random.rand(10,12), columns=mux)
print (df)
        BAC                 JPM                 WFC                   C  \
      Close      Open     Close      Open     Close      Open     Close   
0  0.543405  0.278369  0.424518  0.844776  0.004719  0.121569  0.670749   
1  0.185328  0.108377  0.219697  0.978624  0.811683  0.171941  0.816225   
2  0.175410  0.372832  0.005689  0.252426  0.795663  0.015255  0.598843   
3  0.980921  0.059942  0.890546  0.576901  0.742480  0.630184  0.581842   
4  0.285896  0.852395  0.975006  0.884853  0.359508  0.598859  0.354796   
5  0.376252  0.592805  0.629942  0.142600  0.933841  0.946380  0.602297   
6  0.173608  0.966610  0.957013  0.597974  0.731301  0.340385  0.092056   
7  0.395036  0.335596  0.805451  0.754349  0.313066  0.634037  0.540405   
8  0.254258  0.641101  0.200124  0.657625  0.778289  0.779598  0.610328   
9  0.976500  0.166694  0.023178  0.160745  0.923497  0.953550  0.210978   

                   MS               Other            
       Open     Close      Open     Close      Open  
0  0.825853  0.136707  0.575093  0.891322  0.209202  
1  0.274074  0.431704  0.940030  0.817649  0.336112  
2  0.603805  0.105148  0.381943  0.036476  0.890412  
3  0.020439  0.210027  0.544685  0.769115  0.250695  
4  0.340190  0.178081  0.237694  0.044862  0.505431  
5  0.387766  0.363188  0.204345  0.276765  0.246536  
6  0.463498  0.508699  0.088460  0.528035  0.992158  
7  0.296794  0.110788  0.312640  0.456979  0.658940  
8  0.309000  0.697735  0.859618  0.625324  0.982408  
9  0.360525  0.549375  0.271831  0.460602  0.696162 

First select columns by slicers, then call pct_change and last remove second level of MultiIndex in column by droplevel:

tickers=['BAC','JPM','WFC','C','MS']

idx = pd.IndexSlice
df = df.sort_index(axis=1)
returns = df.loc[:, idx[tickers,'Close']].pct_change()
returns.columns = returns.columns.droplevel(-1)
print (returns)
        BAC         C         JPM        MS         WFC
0       NaN       NaN         NaN       NaN         NaN
1 -0.658950  0.216885   -0.482477  2.157889  171.008452
2 -0.053515 -0.266325   -0.974108 -0.756436   -0.019738
3  4.592146 -0.028390  155.551779  0.997444   -0.066841
4 -0.708544 -0.390220    0.094841 -0.152103   -0.515801
5  0.316048  0.697588   -0.353910  1.039454    1.597555
6 -0.538586 -0.847159    0.519208  0.400649   -0.216890
7  1.275448  4.870415   -0.158370 -0.782213   -0.571905
8 -0.356369  0.129391   -0.751538  5.297934    1.486019
9  2.840595 -0.654320   -0.884181 -0.212630    0.186573

Upvotes: 1

Related Questions