Reputation: 27946
I have dfi
= (i is the index)
i a
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
and dfj
= (j is the index)
j b
0 0
1 1
2 2
3 3
4 4
I want to add an a
column to dfj
such that the output would be dfi
subsampled by a factor of 2
j b a
0 0 0
1 1 2
2 2 4
3 3 6
4 4 8
My attempt:
idx = pd.IndexSlice
dfj["a"] = dfi.loc[idx[::2]]
This fails because dfi.loc[idx[::2]]
returns indices that don't fit for dfj
. Looks like I need to divide the index, or do another workaround.
What's the correct syntax for that?
Thanks
Upvotes: 1
Views: 187
Reputation: 150745
dfi.iloc[::2]
or dfi.loc[idx[::2]]
has different indexes than dfj
, while assignment will align the indexes for you. You can pass the values instead:
dfj['a'] = dfi['a'].iloc[::2].values
Output:
b a
j
0 0 0
1 1 2
2 2 4
3 3 6
4 4 8
Upvotes: 1