import pandas as pd
import numpy as np
df = pd.DataFrame(data = np.random.random(36).reshape((9,4)), index = np.arange(1, np.random.random(36).reshape((9,4)).shape[0]+1), columns=['A', 'B', 'C', 'D'])
te = pd.DataFrame(data = np.random.randint(low=1, high=10, size=1000), columns=['Number_test'])
我想通过df索引将两个数据帧连接到Number_test列中的每个对应元素
答案 0 :(得分:2)
pd.merge(df, te, left_index=True, right_on='Number_test')
或
pd.merge(df.reset_index(), te, left_on='index', right_on='Number_test')