如何将一列中的select个大写单词分隔成一个新的列?
How to select uppercase words from a column and separate into a new column?
我在 1 列中有一个基因和药物数据集,如下所示:
Molecules
3-nitrotyrosine
4-phenylbutyric acid
5-fluorouracil/leucovorin/oxaliplatin
5-hydroxytryptamine
ABCB4
ABCC8
ABCC9
ABCF2
ABHD4
列中基因和药物的散布是随机的,所以我无法精确划分。
我想删除基因并将它们放入一个新列中,我想知道我是否可以使用 isupper() 来 select 基因并将它们移动到一个新列中,尽管我知道这只适用于字符串。有什么方法可以 select 将带有大写字母的行放入新列中吗?任何指导将不胜感激。
Expected Output:
Column 1 Column 2
3-nitrotyrosine ABCB4
4-phenylbutyric acid ABCC8
5-fluorouracil/leucovorin/oxaliplatin ABCC9
5-hydroxytryptamine ABCF2
将您的文件读入列表:
with open('test.txt', 'r') as f:
lines = [line.strip() for line in f]
去掉所有大写字母:
mols = [x for x in lines if x.upper() != x]
genes = [x for x in lines if x.upper() == x]
结果:
mols
['3-nitrotyrosine', '4-phenylbutyric acid',
'5-fluorouracil/leucovorin/oxaliplatin', '5-hydroxytryptamine']
genes
['ABCB4', 'ABCC8', 'ABCC9', 'ABCF2', 'ABHD4']
如前所述,区分大写很简单:
df.loc[df['Molecules'].str.isupper()]
Molecules
5 ABCB4
6 ABCC8
7 ABCC9
8 ABCF2
9 ABHD4
df.loc[df['Molecules'].str.isupper() == False]
Molecules
0 3-nitrotyrosine
1 4-phenylbutyric
2 acid
3 5-fluorouracil/leucovorin/oxaliplatin
4 5-hydroxytryptamine
但是,在您能够提供更多详细信息之前,您还不清楚您希望如何匹配这些行。
我在 1 列中有一个基因和药物数据集,如下所示:
Molecules
3-nitrotyrosine
4-phenylbutyric acid
5-fluorouracil/leucovorin/oxaliplatin
5-hydroxytryptamine
ABCB4
ABCC8
ABCC9
ABCF2
ABHD4
列中基因和药物的散布是随机的,所以我无法精确划分。 我想删除基因并将它们放入一个新列中,我想知道我是否可以使用 isupper() 来 select 基因并将它们移动到一个新列中,尽管我知道这只适用于字符串。有什么方法可以 select 将带有大写字母的行放入新列中吗?任何指导将不胜感激。
Expected Output:
Column 1 Column 2
3-nitrotyrosine ABCB4
4-phenylbutyric acid ABCC8
5-fluorouracil/leucovorin/oxaliplatin ABCC9
5-hydroxytryptamine ABCF2
将您的文件读入列表:
with open('test.txt', 'r') as f:
lines = [line.strip() for line in f]
去掉所有大写字母:
mols = [x for x in lines if x.upper() != x]
genes = [x for x in lines if x.upper() == x]
结果:
mols
['3-nitrotyrosine', '4-phenylbutyric acid',
'5-fluorouracil/leucovorin/oxaliplatin', '5-hydroxytryptamine']
genes
['ABCB4', 'ABCC8', 'ABCC9', 'ABCF2', 'ABHD4']
如前所述,区分大写很简单:
df.loc[df['Molecules'].str.isupper()]
Molecules
5 ABCB4
6 ABCC8
7 ABCC9
8 ABCF2
9 ABHD4
df.loc[df['Molecules'].str.isupper() == False]
Molecules
0 3-nitrotyrosine
1 4-phenylbutyric
2 acid
3 5-fluorouracil/leucovorin/oxaliplatin
4 5-hydroxytryptamine
但是,在您能够提供更多详细信息之前,您还不清楚您希望如何匹配这些行。