【发布时间】:2021-05-22 12:38:09
【问题描述】:
我有 2 个数据帧(df1 &df2)。我想从 df2 中获取每一行,对于特定的 ID 和查询,在找到“查询”字符串后从 df1 中获取该特定 ID 的所有值。唯一的条件是显示的行的“发送者”列值应仅为“支持团队”。
我尝试过类似df1= df1.loc['can we find tigers in Amazon forest?':]
但是得到了keyerror..任何人都可以帮我解决这个问题..
Note:Index in df1 is not sorted as the dataframes are grouped based on ID
df1 =
Index ID Query Sent by
0 76649 Hi Jack
2 76649 Anyone there Jack
3 76649 yes hi Support Team
10 76649 this is Fred from support team Support Team
5 76649 can we find tigers in Amazon forest? Jack
6 76649 yes tigers can be found there Support Team
7 76649 contact forest dept for more Support Team
13 76649 thanks for reaching out Support Team
9 67209 Hello Bianca
4 67209 Anyone there Bianca
11 67209 Hi this is Jim from support team Support Team
12 67209 can we find lions in Amazon forest? Bianca
8 67209 yes lions can be found there Support Team
14 67209 contact forest dept for more Support Team
15 67209 thanks for reaching out Support Team
16 67209 sure that helps..thank you Bianca
df2 =
Index Query ID
0 can we find tigers in Amazon forest? 76649
2 can we find lions in Amazon forest? 67209
3 can we find elephant in Amazon forest? 77832
预期输出:
76649 yes tigers can be found there Support Team
76649 contact forest dept for more Support Team
76649 thanks for reaching out Support Team
67209 yes lions can be found there Support Team
67209 contact forest dept for more Support Team
67209 thanks for reaching out Support Team
【问题讨论】:
标签: python python-3.x pandas dataframe merge