如何将 Pandas 数据框中的列拆分为字母值和数字值?

Dav*_*321 3 python string python-3.x pandas data-cleaning

我有一个数据框:

    Name    Section
1   James   P3
2   Sam     2.5C
3   Billy   T35
4   Sarah   A85
5   Felix   5I
Run Code Online (Sandbox Code Playgroud)

如何将数值拆分为名为 Section_Number 的单独列,并将字母值拆分为 Section_Letter。预期结果

    Name    Section Section_Number  Section_Letter
1   James   P3               3          P
2   Sam     2.5C           2.5          C
3   Billy   T35             35          T
4   Sarah   A85             85          A
5   Felix   5L               5          L
Run Code Online (Sandbox Code Playgroud)

jez*_*ael 5

使用str.replacestr.extract[A-Z]+所有大写字符串:

df['Section_Number'] = df['Section'].str.replace('([A-Z]+)', '')
df['Section_Letter'] = df['Section'].str.extract('([A-Z]+)')
print (df)
    Name Section Section_Number Section_Letter
1  James      P3              3              P
2    Sam    2.5C            2.5              C
3  Billy     T35             35              T
4  Sarah     A85             85              A
5  Felix      5I              5              I
Run Code Online (Sandbox Code Playgroud)

对于seelct也是小写值:

df['Section_Number'] = df['Section'].str.replace('([A-Za-z]+)', '')
df['Section_Letter'] = df['Section'].str.extract('([A-Za-z]+)')
print (df)
    Name Section Section_Number Section_Letter
1  James      P3              3              P
2    Sam    2.5C            2.5              C
3  Billy     T35             35              T
4  Sarah     A85             85              A
5  Felix      5I              5              I
Run Code Online (Sandbox Code Playgroud)