如何将变量作为列名pandas传递

时间:2017-03-23 08:25:16

标签: python-2.7 pandas data-science

我正在使用Python 2.7 我尝试根据变量形式列表创建新列

代号= [ 'BAC', 'JPM', 'WFC', 'C', 'MS'] 返回= pd.DataFrame 在代码中打勾:      返回[剔] = bank_stocks [蜱] [[] 1 '关闭']。pct_change()**

但我收到此错误

TypeError Traceback(最近一次调用最后一次)  in()       2返回= pd.DataFrame       3代码中的勾选: ----> 4返回[tick] = bank_stocks [tick] ['Close']。pct_change()       5

TypeError:'type'对象不支持项目分配

2 个答案:

答案 0 :(得分:1)

你需要的IIUC:

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 

首先按slicers选择列,然后调用pct_change,最后在droplevel列中删除第二级MultiIndex:

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

答案 1 :(得分:1)

您的代码是正确的,但In [73]中的行必须调用数据帧(即pd.DataFrame()),而您通过在DataFrame之后不使用“()”来创建对象。这就是为什么错误是类型对象不支持赋值的原因。