
name Section1 James P32 Sam 2.5C3 Billy T354 Sarah A855 Felix 5I
如何将数值拆分为名为Section_Number的单独列,并将字母值拆分为Section_Letter.
期望的结果
name Section Section_Number Section_Letter1 James P3 3 P2 Sam 2.5C 2.5 C3 Billy T35 35 T4 Sarah A85 85 A5 Felix 5L 5 L解决方法 对于所有大写字符串,使用
str.replace和[A-Z]的 str.extract: 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_Letter1 James P3 3 P2 Sam 2.5C 2.5 C3 Billy T35 35 T4 Sarah A85 85 A5 Felix 5I 5 I 对于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_Letter1 James P3 3 P2 Sam 2.5C 2.5 C3 Billy T35 35 T4 Sarah A85 85 A5 Felix 5I 5 I 总结 以上是内存溢出为你收集整理的如何将列拆分为Pandas数据帧中的列的字母值和数值?全部内容,希望文章能够帮你解决如何将列拆分为Pandas数据帧中的列的字母值和数值?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)