[英]How to select this kind of data from this pandas multi-index dataframe
我正在使用期貨市場數據,這是一個示例多索引數據框:
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
我的問題是我怎么“pythonically”在填寫close
的列df2
從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的方法嗎?
使用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
@jezrael的回答非常好,但是如果對於不熟悉xs
(例如我),我只是想出了一種更復雜的方法來獲得s
:
s= df1.loc[:, idx[:, 'close']]
s.columns = s.columns.droplevel(1)
s = s.unstack().rename('close')
當然,這三行陳述看起來並不那么吸引人。 :D然后我們可以用相同的方式得到df1:
df2 = (df2.drop('close', 1)
.reset_index()
.join(s, on=['contract', 'index'])
.set_index('index')
.rename_axis(None))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.