提取与另一个DF的单个元素相对应的数据框的值
,如下所示
DF1 | Col1 | Col2 | Col3 | Col3 | Col4 |
---|---|---|---|---|---|
row1 | dog | 猫猫 | 鸟 | 树 | 狮子 |
2 | 猫 | 狮子 | 鸟 | 狗 | 树 |
3 | 狮子 | 龙 | 有 | 个熊猫 | col4 |
row | col4 | col4 | col4 | : | ) |
2 | DFS(DF1&amp ; | col5 | df2 | 我 | Ant |
df2 | col1 | col2 | col3 | col4 | col5 |
---|---|---|---|---|---|
row1 | 3.219843 | 1.3631996 | 1.0051135 | 0.89303696 | 0.4313375 |
row2 | 2.8661892 | 1.4396228 | 0.7863044 | 0.539315 | 0.48167187 |
row3 | 2.5679462 | 1.3657334 | 0.9470184 | 0.79186934 | 0.48637152 |
row4 | 3.631389 | 0.94815284 | 0.7561722 | 0.6743943 | 0.5441728 |
row5 | 2.4727197 | 1.5941181 | 1.4069512 | 1.064051 | 0.48297918 |
The string elements of the df1 correspond to the values of df2.对于两个数据框,条件都存在元素(或值)在同一行上不重复的条件。但是可以在不同的行上重复。
例如,Row1 = 3.219843的狗,Row3 = 0.9470184,Row4 = 0.7561722等。
我想将所有唯一元素的值提取到1st df的所有元素中。喜欢:
dog = [3.219843,0.539315,1.3657334]
1.3631996,2.8661892,2.5679462,3.631389,2.4727197]
cat = [
非常感谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您的第一列
df1
和df2
是它们各自df
的索引,我们可以提取中每个唯一动物的值df1
通过使用第一个df
作为掩码从第二个df
中提取所有想要的值(结果是一个带有NaN
的新df
code> 在不相关的单元格中,可以将其翻转使用.stack().values
转换为一维数组)。构建数据帧
首先,创建一些测试数据。请在以后的帖子中以类似的形式提供。这就是 @mozway 在评论中谈论的内容。 非常感谢。
(并不总是有人愿意执行所有必要的复制和粘贴操作来启动和运行数据帧以进行测试。)
提取数据
由于您没有指定所需的数据结构,因此这就是概述的策略上面的
dict
理解:结果如下所示:
Assuming that your first columns
df1
anddf2
are the index of their respectivedf
, we can extract the values for each unique animal indf1
by using the firstdf
as a mask to extract all wanted values from the second one (the result is a newdf
withNaN
in irrelevant cells, which can be turned into a 1-dimensional array with.stack().values
).Construct the dataframes
First of, create some test data. Please provide it in a form like this in future posts. That's what @mozway was talking about in the comments. It is greatly appreciated.
(It's not always the case that somebody is willing to do all the copy-and-pasting necessary to get dataframes up and running for testing.)
Extract the data
Since you didn't specify what data structure you need, this is the strategy outlined above in a
dict
comprehension:The result looks like this:
假设@fsimonjetz提供的输入,您可以 两个dataframes,然后
groupby.agg
作为列表:或,使用中间数据框架:
输出:
Assuming the input kindly provided by @fsimonjetz, you can
stack
both dataframes, thenGroupBy.agg
as list:or, using an intermediate DataFrame:
output: