I have outputs from calculations that is best stored in a Pandas MultiIndex format. For concrete purposes, let us consider the form below (though the actual structure is dictated programmatically)
X Y Z
DATE
2018-01-01 A NaN NaN NaN
B NaN NaN NaN
C NaN NaN NaN
2018-01-02 A NaN NaN NaN
B NaN NaN NaN
C NaN NaN NaN
I want to assign the numpy array outputs to a particular time slice. Say I have
output = np.array([[1,2,3],[2,2,1],[4,2,3]])
so the desired output is
X Y Z
DATE
2018-01-01 A NaN NaN NaN
B NaN NaN NaN
C NaN NaN NaN
2018-01-02 A 1 2 3
B 2 2 1
C 4 2 3
I have tried pandas.IndexSlice where j is the j-th time slice.
df.loc[pd.IndexSlice[j,:], :] = output
but that doesn't work. I have also tried by replacing loc by iloc but to no avail. In non-MultiIndex dataframes, I can assign a list to a particular column in a DataFrame without having to assign each element individually. Is there a way to do it for a matrix into a MultiIndex dataframe?
your code works just fine.
Demo:
In [70]: df.loc[pd.IndexSlice['2018-01-02', :], :] = output
In [71]: df
Out[71]:
X Y Z
DATE I2
2018-01-01 A NaN NaN NaN
B NaN NaN NaN
C NaN NaN NaN
2018-01-02 A 1.0 2.0 3.0
B 2.0 2.0 1.0
C 4.0 2.0 3.0
PS i tested both options when the DATE
index column is of string
and when it's is of datetime
dtype - in both cases the code above is working properly.
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.