gpt4 book ai didi

python - 从 python 中的字母数字列中删除字符串

转载 作者:行者123 更新时间:2023-12-02 15:46:59 24 4
gpt4 key购买 nike

我有一个数据框

import pandas as pd    
data_as_dict={'CHROM': {232: 1, 233: 1, 234: 1, 10: 'chr15', 11: 'chr15'}, 'POS_GRCh38': {232: 10506158, 233: 109655507, 234: 113903258, 10: '67165147', 11: '67163292'}, 'REF': {232: 'G', 233: 'CAAA', 234: 'G', 10: 'G', 11: 'C'}, 'Effect_allele': {232: 'A', 233: 'C', 234: 'A', 10: 'C', 11: 'T'}, 'Effect_size': {232: 0.1109, 233: 0.0266, 234: 0.0579, 10: 0.2070141693843261, 11: 0.2151113796169455}, 'TYPE': {232: 'Mavaddat_2019_ER_NEG_Breast', 233: 'Mavaddat_2019_ER_NEG_Breast', 234: 'Mavaddat_2019_ER_NEG_Breast', 10: 'THYROID_PGS', 11: 'THYROID_PGS'}, 'Cancer': {232: 'Breast', 233: 'Breast', 234: 'Breast', 10: 'THYROID', 11: 'THYROID'}, 'Significant_YN': {232: 'Y', 233: 'Y', 234: 'Y', 10: 'Y', 11: 'Y'}}

all_cancers = pd.DataFrame.from_dict(data_as_dict)

我想从 CHROM 列中删除 chr。我尝试了 all_cancers['CHROM'] = all_cancers['CHROM'].str.replace(r'chr', '') ,它生成 NaN。我知道可以在 R 中使用 gsub 轻松完成,但我想在 python 中尝试。我该如何正确地做到这一点?

最佳答案

使用正则表达式;

import re
all_cancers["CHROM"] = all_cancers["CHROM"].apply(lambda x: re.sub('\D', '', str(x)))

关于python - 从 python 中的字母数字列中删除字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73829933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com