Skip to main content
 首页 » 编程设计

python之使用 Pandas 拆分列并用提取的值填充另一列

2025年04月02日11xxx_UU

class_name 列包含类(class)名称和队列编号。 我想将列拆分为两列(姓名、群组编号)

来自:

| class_name | 
 
| introduction to programming 1th | 
| introduction to programming 2th | 
| introduction to programming 3th | 
| introduction to programming 4th | 
| algorithms and data structure 1th | 
| algorithms and data structure 2th | 
| object-oriented programming | 
| database systems | 

(我知道它应该像 1st、2nd、3rd,但字符串是我的语言,我们在数字后一遍又一遍地使用相同的字符)。

收件人:

| class_name | class_cohort | 
 
| introduction to programming | 1 | 
| introduction to programming | 2 | 
| introduction to programming | 3 | 
| introduction to programming | 4 | 
| algorithms and data structure | 1 | 
| alrogithms and data structure | 2 | 
| object-oriented programming | 1 | 
| database systems | 1 | 

这是我一直在处理的代码:

import pandas as pd 
 
course_count = 100 
df = pd.read_csv("course.csv", nrows=course_count) 
 
cols_interest=['class_name', 'class_department', 'class_type', 'student_target', 'student_enrolled'] 
 
df = df[cols_interest] 
df.insert(1, 'class_cohort', 0) 
 
# this is how I extract the numbers 
df['class_name'].str.extract('(\d)').head() 
 
# but I cannot figure out a way to copy those values into column 'class_cohort' which I filled with 0's. 
 
# once I figure that out, I plan to discard the last digits 
df['class_name'] = df['class_name'].map(lambda x: str(x)[:-1]) 

我简要检查了一个解决方案,我将逗号全部放在第 1、2、3 之前,然后使用逗号作为分隔符拆分列,但我想不出一种方法来替换所有数字的\s1th -> ,1th。

请您参考如下方法:

您可以 indexing by positions :

df['class_cohort'] = df['class_name'].str[-3:-2] 
df['class_name'] = df['class_name'].str[:-4] 
print df 
   class_name class_cohort 
0       cs101            1 
1       cs101            2 
2       cs101            3 
3       cs101            4 
4  algorithms            1 
5  algorithms            2 

或者使用str.extract :

df['class_cohort'] = df['class_name'].str.extract('(\d)') 
df['class_name'] = df['class_name'].str[:-4] 
print df 
                      class_name class_cohort 
0    introduction to programming            1 
1    introduction to programming            2 
2    introduction to programming            3 
3    introduction to programming            4 
4  algorithms and data structure            1 
5  algorithms and data structure            2