Example of how to select the rows of a dataframe from the indices of another dataframe with pandas in python
Table of contents
A first example
Select rows of a dataframe df1 using the indices of a dataframe df2:
df1.loc[df2.index]
A second example
Lets create a dataframe with pandas:
import pandas as pdimport numpy as npimport randomSurface = [random.choice(['Ocean','Snow','Desert', 'Forest']) for i in range(20)]c1 = np.random.uniform(0,1, size=20)c2 = np.random.uniform(0,1, size=20)c3 = np.random.uniform(0,1, size=20)data = {'Surface':Surface,'c1':c1,'c2':c2,'c3':c3}df = pd.DataFrame(data)print(df)
returns for example:
Surface c1 c2 c30 Snow 0.803453 0.293418 0.7111851 Desert 0.152995 0.683300 0.1587482 Desert 0.473076 0.854214 0.8005043 Forest 0.733894 0.703959 0.8249944 Snow 0.666677 0.266554 0.2418215 Forest 0.329957 0.639363 0.8157886 Forest 0.458453 0.327581 0.8786867 Ocean 0.934025 0.270376 0.5920778 Desert 0.978084 0.786852 0.8893069 Forest 0.735452 0.700018 0.65305310 Snow 0.662458 0.565464 0.77982111 Desert 0.342549 0.527397 0.91250912 Desert 0.718146 0.150439 0.63936013 Ocean 0.629078 0.640988 0.85061214 Forest 0.948290 0.348320 0.57973715 Desert 0.888459 0.339470 0.71539616 Ocean 0.685656 0.599464 0.27778117 Desert 0.771131 0.344754 0.77689918 Ocean 0.262968 0.628254 0.75718919 Desert 0.161602 0.367706 0.941437
Create another dataframe by selecting randomly rows a the first dataframe:
df_sample = df[['c1','c2','c3']].sample(n=10, random_state = 42)
returns:
c1 c2 c30 0.803453 0.293418 0.71118517 0.771131 0.344754 0.77689915 0.888459 0.339470 0.7153961 0.152995 0.683300 0.1587488 0.978084 0.786852 0.8893065 0.329957 0.639363 0.81578811 0.342549 0.527397 0.9125093 0.733894 0.703959 0.82499418 0.262968 0.628254 0.75718916 0.685656 0.599464 0.277781
Lets now retrieve the column "surface" for the rows of the dataframe df_sample:
df.loc[df_sample.index]
gives
Surface c1 c2 c30 Snow 0.803453 0.293418 0.71118517 Desert 0.771131 0.344754 0.77689915 Desert 0.888459 0.339470 0.7153961 Desert 0.152995 0.683300 0.1587488 Desert 0.978084 0.786852 0.8893065 Forest 0.329957 0.639363 0.81578811 Desert 0.342549 0.527397 0.9125093 Forest 0.733894 0.703959 0.82499418 Ocean 0.262968 0.628254 0.75718916 Ocean 0.685656 0.599464 0.277781
