我正在处理期货市场数据,这是一个多索引数据框的示例:
date_index = pd.date_range('2018-03-20', periods = 10)
contract = ['ZN1805', 'ZN1806', 'ZN1807']
price = ['open', 'close']
columns = pd.MultiIndex.from_product([contract, price], names=['contract', 'price'])
df1 = pd.DataFrame(data=np.random.randint(100, 150, (10, columns.shape[0])), index=date_index, columns=columns)
df2 = pd.DataFrame(columns=['contract', 'close'], index=df1.index)
# Set the data in contract column randomly here for illustration
df2.contract = np.random.choice(contract, 10)
以下是df1
的样子,
df1
Out[357]:
contract ZN1805 ZN1806 ZN1807
price open close open close open close
2018-03-20 145 144 116 127 107 128
2018-03-21 116 143 114 103 114 148
2018-03-22 101 135 143 125 140 129
2018-03-23 106 139 100 127 116 100
2018-03-24 104 101 148 132 102 140
2018-03-25 125 141 106 136 128 134
2018-03-26 148 146 142 143 108 137
2018-03-27 110 123 128 128 124 127
2018-03-28 144 143 117 116 112 140
2018-03-29 143 114 115 105 124 118
和df2
将是:
df2
Out[364]:
contract close
2018-03-20 ZN1805 NaN
2018-03-21 ZN1807 NaN
2018-03-22 ZN1806 NaN
2018-03-23 ZN1807 NaN
2018-03-24 ZN1807 NaN
2018-03-25 ZN1806 NaN
2018-03-26 ZN1807 NaN
2018-03-27 ZN1806 NaN
2018-03-28 ZN1805 NaN
2018-03-29 ZN1807 NaN
我的问题是我如何诡异地'填写close
df2
的{{1}}列df1
列,其date
索引和contract
值相同?
我试过了:
from pandas import IndexSlice as idx
df2['close'] = df1.loc[df2.index, idx[df2.contract.values.tolist(), 'close']]
但是我收到了一个错误:
UnsortedIndexError: 'MultiIndex Slicing requires the index to be fully lexsorted tuple len (2), lexsort depth (1)'
我知道我可以用迭代的方式来过滤每一行,但是有任何pythonic方法可以做到吗?
答案 0 :(得分:2)
使用join
创建的2列xs
选择close
级别,unstack
重新塑造:
s = df1.xs('close', axis=1, level=1).unstack().rename('close')
df2 = (df2.drop('close', 1)
.reset_index()
.join(s, on=['contract', 'index'])
.set_index('index')
.rename_axis(None))
print (df2)
contract close
2018-03-20 ZN1805 124
2018-03-21 ZN1805 112
2018-03-22 ZN1807 118
2018-03-23 ZN1807 136
2018-03-24 ZN1805 103
2018-03-25 ZN1805 135
2018-03-26 ZN1805 138
2018-03-27 ZN1805 109
2018-03-28 ZN1805 129
2018-03-29 ZN1805 104
答案 1 :(得分:0)
xs
的人(像我一样),我会想出一个更复杂的方法来获得s
:
s= df1.loc[:, idx[:, 'close']]
s.columns = s.columns.droplevel(1)
s = s.unstack().rename('close')
当然,这个3行声明并不具有吸引力。 :d 然后我们可以用同样的方式得到df1:
df2 = (df2.drop('close', 1)
.reset_index()
.join(s, on=['contract', 'index'])
.set_index('index')
.rename_axis(None))