class_name列同时包含课程名称和同类群组编号。
我想将栏分为两栏(名称,同类群组编号)
从:
| 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。
最佳答案
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