数据帧中的匹配列基于来自其他数据帧的列中的值

2024-05-29 03:08:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个数据帧 第一个是df1有485513列100行

head(df1)

sample  cg1 cg2 cg3 cg4 cg5 cg6 cg7 cg8 cg9 cg10    cg11
AAD_1   33435   33436   33437   33438   33439   33440   33441   33442   33443   33444   33445
AAD_2   0.33    1.33    2.33    3.33    4.33    5.33    6.33    7.33    8.33    9.33    10.33
AAD_3   0.56    1.56    2.56    3.56    4.56    5.56    6.56    7.56    8.56    9.56    10.56
AAD_4   45.9    46.9    47.9    48.9    49.9    50.9    51.9    52.9    53.9    54.9    55.9
AAD_5   46.9    47.9    48.9    49.9    50.9    51.9    52.9    53.9    54.9    55.9    56.9
AAD_6   47.9    48.9    49.9    50.9    51.9    52.9    53.9    54.9    55.9    56.9    57.9
AAD_7   48.9    49.9    50.9    51.9    52.9    53.9    54.9    55.9    56.9    57.9    58.9
AAD_8   49.9    50.9    51.9    52.9    53.9    54.9    55.9    56.9    57.9    58.9    59.9
AAD_9   50.9    51.9    52.9    53.9    54.9    55.9    56.9    57.9    58.9    59.9    60.9
AAD_10  51.9    52.9    53.9    54.9    55.9    56.9    57.9    58.9    59.9    60.9    61.9

第二个是df284行单列。我的目标是使用df2数据帧列中的值来获得df1的子集。在

^{pr2}$

df2的值是df1中我感兴趣的列名,因此我在R中尝试了以下一个行程序

> UP=(df1 %>% as.data.frame)[,df2$ID]

Up数据帧从查询df2返回不匹配的列

它产生了一个包含84列100行的数据帧,但是上面的命令行返回的列没有一个与输入查询数据帧df2匹配。在

如果有人能给我一个替代方案那就太好了


Tags: 数据sampleheaddf1df2aadcg4cg2
2条回答

假设df2是一个序列:

>>> df[df2.tolist()]

        cg1       cg2       cg3       cg4       cg5
0  33435.00  33436.00  33437.00  33438.00  33439.00
1      0.33      1.33      2.33      3.33      4.33
2      0.56      1.56      2.56      3.56      4.56
3     45.90     46.90     47.90     48.90     49.90
4     46.90     47.90     48.90     49.90     50.90
5     47.90     48.90     49.90     50.90     51.90
6     48.90     49.90     50.90     51.90     52.90
7     49.90     50.90     51.90     52.90     53.90
8     50.90     51.90     52.90     53.90     54.90
9     51.90     52.90     53.90     54.90     55.90

如果它是一个数据帧,那么它应该可以工作:

^{pr2}$

R中,我们可以

df[as.character(df2$ID)]

假设'ID'列是factor。如果是character类,则更容易

^{pr2}$

但是如果'ID'中有不在'df'列名中的元素,则最好使用intersect

df[intersect(colnames(df), df2$ID)]

如果'df'是data.table,通常的列子集方法是包括with =FALSE。它在?data.table中提到过

使用

By default with=TRUE and j is evaluated within the frame of x; column names can be used as variables.

When with=FALSE j is a character vector of column names, a numeric vector of column positions to select or of the form startcol:endcol, and the value returned is always a data.table. with=FALSE is often useful in data.table to select columns dynamically. Note that x[, cols, with=FALSE] is equivalent to x[, .SD, .SDcols=cols].

因此,上面的命令将是

^{4}$

或者

 df[, df2$ID, with = FALSE] #if 'ID' is already character class.

或者

 df[, intersect(colnames(df), df2$ID), with = FALSE]

相关问题 更多 >

    热门问题