Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.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
Python,如何通过查找或.map来交互两个不同的数据帧 Python,如何在一个for循环中交互两个不同的数据帧_Python_Pandas_Numpy - Fatal编程技术网

Python,如何通过查找或.map来交互两个不同的数据帧 Python,如何在一个for循环中交互两个不同的数据帧

Python,如何通过查找或.map来交互两个不同的数据帧 Python,如何在一个for循环中交互两个不同的数据帧,python,pandas,numpy,Python,Pandas,Numpy,大家好,我有两个数据帧 data-frame1=包含成本ID data-frame2=包含ID 我想做一个for循环,它将查找data-frame2的ID并作为一个新列返回Cost 问题。。他们没有一些长度 同样,for循环未找到ID的 因此,他们都将拥有数据,例如: data-frame1=包含ID和成本 # data-frame 1 cw.head() campaignid Month 0 31664745 2019|08 1 85264521 2

大家好,我有两个数据帧

data-frame1=包含成本ID data-frame2=包含ID 我想做一个for循环,它将查找data-frame2的ID并作为一个新列返回Cost

问题。。他们没有一些长度 同样,for循环未找到ID的 因此,他们都将拥有数据,例如:

data-frame1=包含ID和成本
# data-frame 1

cw.head()

    campaignid  Month   

0   31664745    2019|08 
1   85264521    2019|08 
2   85264521    2019|08 
3   85264521    2019|08 
4   31664745    2019|08 
5   31664745    2019|08 
6   31664745    2019|08 
7   85264521    2019|08 
8   31664745    2019|08 
9   31664745    2019|08 
10  85264521    2019|08 



# data-frame 2

g_spend.head()

    campaignid  cpl

0   31664745    41  
1   85264521    55


data-frame2=包含ID和成本
# data-frame 1

cw.head()

    campaignid  Month   

0   31664745    2019|08 
1   85264521    2019|08 
2   85264521    2019|08 
3   85264521    2019|08 
4   31664745    2019|08 
5   31664745    2019|08 
6   31664745    2019|08 
7   85264521    2019|08 
8   31664745    2019|08 
9   31664745    2019|08 
10  85264521    2019|08 



# data-frame 2

g_spend.head()

    campaignid  cpl

0   31664745    41  
1   85264521    55


我的尝试 预期结果
如果
活动ID
g_-expense
中是唯一的,您可以使用
map
更新其他数据:

cw['cpl'] = cw['campaignid'].map(g_spend.set_index('campaignid')['cpl'])
输出:

    campaignid    Month  cpl
0     31664745  2019|08   41
1     85264521  2019|08   55
2     85264521  2019|08   55
3     85264521  2019|08   55
4     31664745  2019|08   41
5     31664745  2019|08   41
6     31664745  2019|08   41
7     85264521  2019|08   55
8     31664745  2019|08   41
9     31664745  2019|08   41
10    85264521  2019|08   55

如果您不需要使用循环,您可以尝试将
merge
Pandas
合并。谢谢@Hatt,但这不起作用,因为它们的长度不同,而且由于其他内部原因,我无法合并它们。默认情况下,只需执行查找
合并
,不关心长度。对于这个问题,这的确是一个很好的选择。谢谢你帮助@Quang,我有错误。“cpl”的键错误”和“TypeError:“DataFrame”对象不可调用”您能否检查
键错误
:您的
g_expense.head()
有一个
cpl
列。
    campaignid    Month  cpl
0     31664745  2019|08   41
1     85264521  2019|08   55
2     85264521  2019|08   55
3     85264521  2019|08   55
4     31664745  2019|08   41
5     31664745  2019|08   41
6     31664745  2019|08   41
7     85264521  2019|08   55
8     31664745  2019|08   41
9     31664745  2019|08   41
10    85264521  2019|08   55