在单元格中的第一个字母之后将pandas dataframe列拆分为两列

时间:2018-07-09 10:44:53

标签: python pandas

问题

我想将pandas数据框中的一列分为2列,在percent列中(请参见下文),每个条目都以大写字母字符开头,我想在此字母后立即将'Percentage'列拆分,新列标记为“氨基酸”。

当前代码:

import pandas as pd

df = pd.read_csv('foo.csv')

df['Amino Acid'], df['Percentage'] = zip(*df['Percentage'].map(lambda x: x.split('[^a-zA-Z]')))

df.to_csv('bar.csv',index=False)

输入数据示例

+-----------------------------+-------+-----+-----------+---------------------------------------------------------------------------------------------+
|           Species           |  ID   | OGT |    DB     |                                         Percentage                                          |
+-----------------------------+-------+-----+-----------+---------------------------------------------------------------------------------------------+
| Halogeometricum borinquense | 60847 |  37 | ATCC/DSMZ | E is 8.333003365670164% in ./archaea/GCF_000337855.1/GCF_000337855.1_ASM33785v1_protein.faa |
| Halogeometricum borinquense | 60847 |  37 | ATCC/DSMZ | R is 6.310991522830762% in ./archaea/GCF_000337855.1/GCF_000337855.1_ASM33785v1_protein.faa |
| Halogeometricum borinquense | 60847 |  37 | ATCC/DSMZ | A is 10.22668778459711% in ./archaea/GCF_000337855.1/GCF_000337855.1_ASM33785v1_protein.faa |
+-----------------------------+-------+-----+-----------+---------------------------------------------------------------------------------------------+

所需输出示例

+-----------------------------+-------+-----+-----------+------------+--------------------------------------------------------------------------------------------+
|           Species           |  ID   | OGT |    DB     | Amino Acid |                                         Percentage                                         |
+-----------------------------+-------+-----+-----------+------------+--------------------------------------------------------------------------------------------+
| Halogeometricum borinquense | 60847 |  37 | ATCC/DSMZ | E          |  is 8.333003365670164% in ./archaea/GCF_000337855.1/GCF_000337855.1_ASM33785v1_protein.faa |
| Halogeometricum borinquense | 60847 |  37 | ATCC/DSMZ | R          | is 6.310991522830762% in ./archaea/GCF_000337855.1/GCF_000337855.1_ASM33785v1_protein.faa  |
| Halogeometricum borinquense | 60847 |  37 | ATCC/DSMZ | A          | is 10.22668778459711% in ./archaea/GCF_000337855.1/GCF_000337855.1_ASM33785v1_protein.faa  |
+-----------------------------+-------+-----+-----------+------------+--------------------------------------------------------------------------------------------+

2 个答案:

答案 0 :(得分:5)

您可以直接提取第一个字母:

df['Amino Acid'] = df['Percentage'].str[0]
df['Percentage'] = df['Percentage'].str[1:]

答案 1 :(得分:5)

使用split作为第一个空格:

df[['Amino Acid', 'Percentage']] = df['Percentage'].str.split(n=1, expand=True)