I'm working with futures market data, here is an example 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)
Here is what df1
looks like,
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
and df2
would be:
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
My problem is how do I 'pythonically' fill in the close
column of df2
from df1
that has the same date
index and contract
value?
I tried this:
from pandas import IndexSlice as idx
df2['close'] = df1.loc[df2.index, idx[df2.contract.values.tolist(), 'close']]
However I got an error:
UnsortedIndexError: 'MultiIndex Slicing requires the index to be fully lexsorted tuple len (2), lexsort depth (1)'
I understand I could do an iterated way to filter each row, but any pythonic way to do it?
Use join
by 2 columns created by xs
for select close
level and unstack
for 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
@jezrael's answer is very good, but if for someone who is not familiar with xs
(like me), I just come up with a more complex way to get s
first:
s= df1.loc[:, idx[:, 'close']]
s.columns = s.columns.droplevel(1)
s = s.unstack().rename('close')
Of course this 3-line statement doesn't look that attractive. :D Then we can get df1 in the same way:
df2 = (df2.drop('close', 1)
.reset_index()
.join(s, on=['contract', 'index'])
.set_index('index')
.rename_axis(None))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.