使用列(字符串数据类型)的值筛选

2024-05-19 19:28:41 发布

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

我一直在研究一个大型基因组学数据集,该数据集包含对每个样本的多次读取,以确保我们获得数据,但在分析数据时,我们需要将其降到一行,这样我们就不会扭曲数据(当一个实例被多次读取时,将该基因计算为存在6次)。每一行都有一个ID,因此我在ID上使用了pandasdf.groupby()函数。下面是一个表,试图说明我想做什么:

# ID   |  functionality   |   v_region_score   |   constant_region 
# -----------------------------------------------------------------
# 123  |  productive      |      820           |      NaN
#      |  unknown         |      720           |      NaN
#      |  unknown         |      720           |      IgM
# 456  |  unknown         |      690           |      NaN
#      |  unknown         |      670           |      NaN
# 789  |  productive      |      780           |      IgM
#      |  productive      |      780           |      NaN

(编辑)以下是示例数据帧的代码:

df1 = pd.DataFrame([
    [789, "productive", 780, "IgM"],
    [123, "unknown", 720, np.nan],
    [123, "unknown", 720, "IgM"],
    [789, "productive", 780, np.nan],
    [123, "productive", 820, np.nan],
    [456, "unknown", 690, np.nan],
    [456, "unknown", 670, np.nan]], 
    columns=["ID", "functionality", "v_region_score", "constant_region"])

这将是选择正确行的最终输出:

df2 = pd.DataFrame([
    [789, "productive", 780, "IgM"],
    [123, "productive", 820, np.nan],
    [456, "unknown", 690, np.nan]], 
    columns=["ID", "functionality", "v_region_score", "constant_region"])

所以分组后,对于每个组,如果它在功能上有一个“生产性”值,我想保留该行,如果它是“未知”的,我取最高的v_区域值,如果有多个“生产性”值,我取在其恒定区域中有一些值的值

我尝试了几种访问这些值的方法:

id, frame = next(iter(df_grouped))

if frame["functionality"].equals("productive"):
    # do something

只看一组:

x = df_grouped.get_group("1:1101:10897:22442")

for index, value in x["functionality"].items():
    print(value)

# returns the correct value and type "str"

甚至将每组放入一个列表:

new_groups = []

for id, frame in df_grouped:
    new_groups.append(frame)

# access a specific index returns a dataframe
new_groups[30]

所有这些的错误是“序列的真值是模糊的”,我现在理解了为什么这不起作用,但我不能使用a.any()a.all()a.bool(),因为条件是多么复杂

是否有任何方法可以根据列的值在每个组中选择特定的行?很抱歉问了这么复杂的问题,请提前感谢!:)


Tags: 数据iddfnpnanframeregionunknown
1条回答
网友
1楼 · 发布于 2024-05-19 19:28:41

您可以从不同的角度来处理您的问题:

  1. 根据您的条件对值进行排序
  2. 分组依据ID
  3. 保留每个分组的第一个结果ID

例如:

df1 = df1.sort_values(['ID','functionality','v_region_score','constant_region'], ascending=[True,True,False,True], na_position='last')

df1.groupby('ID').first().reset_index()

Out[0]:
    ID functionality  v_region_score constant_region
0  123    productive             820             IgM
1  456       unknown             690             NaN
2  789    productive             780             IgM

此外,如果要在null时合并constant_region中的值,可以使用fillna(method='ffill'),以便保留存在的值:

## sorted here

df1['constant_region'] = df1.groupby('ID')['constant_region'].fillna(method='ffill')

df1
Out[1]: 
    ID functionality  v_region_score constant_region
4  123    productive             820             NaN
2  123       unknown             720             IgM
1  123       unknown             720             IgM
5  456       unknown             690             NaN
6  456       unknown             670             NaN
0  789    productive             780             IgM
3  789    productive             780             IgM

## Group by here

相关问题 更多 >