Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pandas 按列名查找_Pandas_Lookup - Fatal编程技术网

Pandas 按列名查找

Pandas 按列名查找,pandas,lookup,Pandas,Lookup,如何使用一列(X)使用另一个数据帧(df_2)的列标题查找值 例如: df_1 = pd.DataFrame({'X' : [ 1, 2, 1, 1, 1, 2, 1, 2, 2, 1]}) df_2 = pd.DataFrame({'1' : ['a','b','c','d','e','f','g','h','i','j'], '2' : ['b','c','d','e','f','g','h','i','j','a']}) 我的

如何使用一列(X)使用另一个数据帧(df_2)的列标题查找值

例如:

df_1 = pd.DataFrame({'X' : [ 1,  2,  1,  1,  1,  2,  1,  2,  2,  1]})

df_2 = pd.DataFrame({'1' : ['a','b','c','d','e','f','g','h','i','j'],
                     '2' : ['b','c','d','e','f','g','h','i','j','a']})
我的目标是:

                             a   c   c   d   e   g   g   i   j   j
我试过:

for index, row in df_1.iterrows():
    df_1['Y'] = df_2[df_1['X'][index]]
但我得到了:

KeyError: 1
与铸造柱
X
一起使用,以形成字符串,谢谢@Zero:

a = df_2.lookup(df_2.index, df_1.X.astype(str))
print (a)
['a' 'c' 'c' 'd' 'e' 'g' 'g' 'i' 'j' 'j']

以下是对您方法的改进: