简体   繁体   中英

Index a Pandas DataFrame/Series by Series of indices

I am trying to find a way to create a pandas Series which is based on values within another DataFrame. A simplified example would be:

df_idx = pd.DataFrame([0, 2, 2, 3, 1, 3])
df_lookup = pd.DataFrame([10.0, 20.0, 30.0, 40.0])

where I wish to generate a new pandas series of values drawn from df_lookup based on the indices in df_idx, ie:

df_target = pd.DataFrame([10.0, 30.0, 30.0, 40.0, 20.0, 40.0])

Clearly, it is desirable to do this without looping for speed.

Any help greatly appreciated.

This is what reindex is for:

df_idx = pd.DataFrame([0, 2, 2, 3, 1, 3])
df_lookup = pd.DataFrame([10.0, 20.0, 30.0, 40.0])

df_lookup.reindex(df_idx[0])

Output:

      0
0      
0  10.0
2  30.0
2  30.0
3  40.0
1  20.0
3  40.0

This is precisely the use case for iloc :

import pandas as pd

df = pd.DataFrame([10.0, 20.0, 30.0, 40.0])
idx_lst = pd.Series([0, 2, 2, 3, 1, 3])

res = df.iloc[idx_lst]

See here for more on indexing by position.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM