按索引/列元素连接数据帧

时间:2013-12-06 13:19:21

标签: python pandas

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列中的每个对应元素

1 个答案:

答案 0 :(得分:2)

使用pandas.DataFrame.merge

pd.merge(df, te, left_index=True, right_on='Number_test')

pd.merge(df.reset_index(), te, left_on='index', right_on='Number_test')