Reputation: 455
In the following pandas dataframe (df
) I would like to set the index equal to the values of column "Order" and then reindex such that, in between the index values to have also the following index values (order_adj
), with nans accompanying them in the columns of the dataframe:
order_adj = [30, 182]
ID Date Order Values
16672 22000118 2019-11-06 9 0.320934
16673 22000118 2019-11-06 44 0.213926
16674 22000118 2019-11-06 72 0.178836
16675 22000118 2019-11-06 135 0.182151
16676 22000118 2019-11-06 226 0.162838
I set the index of the dataframe using the following command:
df.index = df.Order.values
ID Date Order Values
9 22000118 2019-11-06 9 0.320934
44 22000118 2019-11-06 44 0.213926
72 22000118 2019-11-06 72 0.178836
135 22000118 2019-11-06 135 0.182151
226 22000118 2019-11-06 226 0.162838
But when I am using df = df.reindex(index = order_adj)
instead of receiving:
ID Date Order Values
9 22000118 2019-11-06 9 0.320934
30 nan nan nan nan
44 22000118 2019-11-06 44 0.213926
72 22000118 2019-11-06 72 0.178836
135 22000118 2019-11-06 135 0.182151
182 nan nan nan nan
226 22000118 2019-11-06 226 0.162838
I receive
ID Date Order Values
30 nan naT nan nan
182 nan naT nan nan
What exactly am I missing?
Upvotes: 0
Views: 386
Reputation: 24314
You can try:
df=df.reindex([*df.index,*order_adj]).sort_index()
OR
df=df.reindex(df.index.tolist()+order_adj).sort_index()
output of df
:
ID Date Order Values
9 22000118.0 2019-11-06 9.0 0.320934
30 NaN NaN NaN NaN
44 22000118.0 2019-11-06 44.0 0.213926
72 22000118.0 2019-11-06 72.0 0.178836
135 22000118.0 2019-11-06 135.0 0.182151
182 NaN NaN NaN NaN
226 22000118.0 2019-11-06 226.0 0.162838
Upvotes: 1
Reputation: 23217
You can use Index.union()
to combine the index with order_adj
in correct sequence. Then .reindex()
, as follows:
idx = df.index.union(order_adj)
df = df.reindex(idx)
or do it in one step, as follows:
df = df.reindex(df.index.union(order_adj))
Result:
print(df)
ID Date Order Values
9 22000118.0 2019-11-06 9.0 0.320934
30 NaN NaN NaN NaN
44 22000118.0 2019-11-06 44.0 0.213926
72 22000118.0 2019-11-06 72.0 0.178836
135 22000118.0 2019-11-06 135.0 0.182151
182 NaN NaN NaN NaN
226 22000118.0 2019-11-06 226.0 0.162838
Upvotes: 0