使用列的值(字符串数据类型)过滤 pandas 组
Filtering pandas groupby using value of column (string datatype)
我一直在研究一个大型基因组学数据集,其中包含每个样本的多次读取,以确保我们获得了数据,但是在分析它时,我们需要将它放到一行中,这样我们就不会倾斜数据(将基因计算为存在 6 次,而实际上它是一个实例多次读取)。每行都有一个 ID,所以我在 ID 上使用了 pandas df.groupby()
函数。这是一个 table 来尝试说明我想做什么:
# 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"])
所以在分组之后,对于每个组,如果它在功能上具有 "productive" 值,我想保留该行,如果它是 "unknown",我取最高的 v_region_score,并且如果有多个 "productive" 值,我会选择在其 constant_region 中具有一定价值的那个。
我尝试了几种访问这些值的方法:
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]
我遇到的所有这些错误是 "The truth value of a Series is ambiguous",我现在明白为什么这不起作用,但我不能使用 a.any()
、a.all()
或 a.bool()
因为条件有多复杂。
有什么方法可以根据列的值在每个组中选择特定的行?很抱歉提出这么复杂的问题,在此先感谢您! :)
您可以从不同的角度解决您的问题:
- 根据您的条件对值进行排序
- 分组依据
ID
- 保留每个分组的第一个结果
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
此外,如果您想合并 constant_region
中的值,当它是 null
时,您可以使用 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
我一直在研究一个大型基因组学数据集,其中包含每个样本的多次读取,以确保我们获得了数据,但是在分析它时,我们需要将它放到一行中,这样我们就不会倾斜数据(将基因计算为存在 6 次,而实际上它是一个实例多次读取)。每行都有一个 ID,所以我在 ID 上使用了 pandas df.groupby()
函数。这是一个 table 来尝试说明我想做什么:
# 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"])
所以在分组之后,对于每个组,如果它在功能上具有 "productive" 值,我想保留该行,如果它是 "unknown",我取最高的 v_region_score,并且如果有多个 "productive" 值,我会选择在其 constant_region 中具有一定价值的那个。
我尝试了几种访问这些值的方法:
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]
我遇到的所有这些错误是 "The truth value of a Series is ambiguous",我现在明白为什么这不起作用,但我不能使用 a.any()
、a.all()
或 a.bool()
因为条件有多复杂。
有什么方法可以根据列的值在每个组中选择特定的行?很抱歉提出这么复杂的问题,在此先感谢您! :)
您可以从不同的角度解决您的问题:
- 根据您的条件对值进行排序
- 分组依据
ID
- 保留每个分组的第一个结果
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
此外,如果您想合并 constant_region
中的值,当它是 null
时,您可以使用 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