Python 从其他列中选择值的数据帧列
这里已经有很多类似的查询,但似乎找不到符合我需要的查询。我有一个pandas数据框架,它主要包含合同名称的列,以及包含许多合同价格数据的值Python 从其他列中选择值的数据帧列,python,pandas,dataframe,calculated-columns,Python,Pandas,Dataframe,Calculated Columns,这里已经有很多类似的查询,但似乎找不到符合我需要的查询。我有一个pandas数据框架,它主要包含合同名称的列,以及包含许多合同价格数据的值 df_yldchg.head(3) Out[392]: ticker RXH16 Comdty RXM16 Comdty RXU16 Comdty IKH16 Comdty IKM16 Comdty \ field PX_LAST PX_LAST PX_LAST PX_LAST PX_L
df_yldchg.head(3)
Out[392]:
ticker RXH16 Comdty RXM16 Comdty RXU16 Comdty IKH16 Comdty IKM16 Comdty \
field PX_LAST PX_LAST PX_LAST PX_LAST PX_LAST
date
2016-01-04 NaN NaN NaN NaN NaN
2016-01-05 1.875658 1.960696 2.538501 4.386991 3.393998
2016-01-06 4.596182 4.729410 5.814771 2.722861 2.102244
ticker IKU16 Comdty contract_month year rolled_IK rolled_RX
field PX_LAST
date
2016-01-04 NaN H 16 IKH16 Comdty RXH16 Comdty
2016-01-05 4.065628 H 16 IKH16 Comdty RXH16 Comdty
2016-01-06 2.523455 H 16 IKH16 Comdty RXH16 Comdty
我希望添加一个名为“RX_yldchange”的列,它在“rolled_RX”列中查找,将其与列标题匹配,并从该列(同一行)中获取值
列名和数据来自数据库查询,并将随着更新时间的推移使用新列进行更新,因此我需要调用与动态检查所有列兼容,而不是硬编码。这类似于excel中的“index(Match())”调用,我相信有一种简单的方法可以做到这一点,但我已经在这里查看了很多问题,但没有找到答案
EDIT2:我已经尝试了一个查找调用,我在这里的其他地方找到了该调用:
----> 1 df_yldchg['RX_yldchange'] = df_yldchg.lookup(df_yldchg.index,
df_yldchg['rolled_RX'])
~\Anaconda3\lib\site-packages\pandas\core\frame.py in lookup(self,
row_labels, col_labels)
3474 result = np.empty(n, dtype='O')
3475 for i, (r, c) in enumerate(zip(row_labels, col_labels)):
-> 3476 result[i] = self._get_value(r, c)
3477
3478 if is_object_dtype(result):
~\Anaconda3\lib\site-packages\pandas\core\frame.py in _get_value(self, index,
col, takeable)
2530 return com._maybe_box_datetimelike(series._values[index])
2531
-> 2532 series = self._get_item_cache(col)
2533 engine = self.index._engine
2534
~\Anaconda3\lib\site-packages\pandas\core\generic.py in _get_item_cache(self,
item)
2484 res = cache.get(item)
2485 if res is None:
-> 2486 values = self._data.get(item)
2487 res = self._box_item_values(item, values)
2488 cache[item] = res
~\Anaconda3\lib\site-packages\pandas\core\internals.py in get(self, item,
fastpath)
4124 raise ValueError("cannot label index with a null
key")
4125
-> 4126 return self.iget(loc, fastpath=fastpath)
4127 else:
4128
~\Anaconda3\lib\site-packages\pandas\core\internals.py in iget(self, i,
fastpath)
4141 Otherwise return as a ndarray
4142 """
-> 4143 block = self.blocks[self._blknos[i]]
4144 values = block.iget(self._blklocs[i])
4145 if not fastpath or not block._box_to_block_values or values.ndim
!= 1:
TypeError: only integer scalar arrays can be converted to a scalar index
如果这不是一个多索引数据帧,这是否可能工作?我很高兴去掉第二层列名('PX_LAST'),如果这有帮助的话
@屏幕摊铺机:
df_yldchg.columns
Out[413]:
MultiIndex(levels=[['IKH16 Comdty', 'IKM16 Comdty', 'IKU16 Comdty', 'RXH16
Comdty', 'RXM16 Comdty', 'RXU16 Comdty', 'contract_month', 'year',
'rolled_IK', 'rolled_RX'], ['PX_LAST', '']],
labels=[[3, 4, 5, 0, 1, 2, 6, 7, 8, 9], [0, 0, 0, 0, 0, 0, 1, 1,
1, 1]],
names=['ticker', 'field'])
提前谢谢
df['RX_yldchange']=df.lookup(df.index,df.rolled_RX)
谢谢-我确实尝试过这个,但我得到了一个错误-将编辑OP来演示。df.columns打印什么?哦,对不起,没有意识到您的数据帧的名称不同。将其更改为df_yldchg['RX_yldchange']=df_yldchg.lookup(df_yldchg.index,df_yldchg['rolled_RX'])
。您需要参考适当的数据帧
,谁知道什么是df