gpt4 book ai didi

python - 如何从 pandas 多索引数据框中选择此类数据

转载 作者:行者123 更新时间:2023-12-01 01:58:33 25 4
gpt4 key购买 nike

我正在处理 future 市场数据,这是一个多索引数据框示例:

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

我的问题是如何“Python方式”填写具有相同日期的df1df2close 索引和合约 值?

我尝试过这个:

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方式可以做到这一点吗?

最佳答案

使用joinxs 创建的 2 列选择关闭级别和unstack reshape :

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

关于python - 如何从 pandas 多索引数据框中选择此类数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49919252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com