我在名为ms_sample_id
的pandas数据框中有一个列selected_id_df
。唯一的行由
selected_id_df.ms_sample_id.unique()
哪个收益率:
array(['mitra_baseline_310808-1', 'mitra_baseline_310808-2',
'mitra_baseline_310808-3', 'mitra_baseline_310808-4',
'mitra_baseline_310907-1', 'mitra_baseline_310907-2',
'mitra_baseline_310907-3', 'mitra_baseline_310907-4',
'mitra_baseline_311090-1', 'mitra_baseline_311090-2',
'mitra_baseline_311090-3', 'mitra_baseline_311090-4',
'mitra_baseline_311091-1', 'mitra_baseline_311091-2',
'mitra_baseline_311091-3', 'mitra_baseline_311091-4',
'mitra_baseline_311123-1', 'mitra_baseline_311123-2',
'mitra_baseline_311123-3', 'mitra_baseline_311123-4',
'frozen-2w_310808-1', 'frozen-2w_310808-2', 'frozen-2w_310907-1',
'frozen-2w_310907-2', 'frozen-2w_311090-1', 'frozen-2w_311090-2',
'frozen-2w_311091-1', 'frozen-2w_311091-2', 'frozen-2w_311123-1',
'frozen-2w_311123-2', 'RT-2w_310808-1', 'RT-2w_310808-2',
'RT-2w_310907-1', 'RT-2w_310907-2', 'RT-2w_311090-1',
'RT-2w_311090-2', 'RT-2w_311091-1', 'RT-2w_311091-2',
'RT-2w_311123-1', 'RT-2w_311123-2', 'LT_RT_310808_1',
'LT_RT_310808_2', 'LT_RT_310907_1', 'LT_RT_310907_2',
'LT_RT_311090_1', 'LT_RT_311090_2', 'LT_RT_311091_1',
'LT_RT_311091_2', 'LT_RT_311123_1', 'LT_RT_311123_2',
'LT-frozen_310808_1', 'LT-frozen_310808_2', 'LT-frozen_310907_1',
'LT-frozen_310907_2', 'LT-frozen_311090_1', 'LT-frozen_311090_2',
'LT-frozen_311091_1', 'LT-frozen_311091_2', 'LT-frozen_311123_1',
'LT-frozen_311123_2'], dtype=object)
我想用-
替换ID中的部分_
。我这样做如下:
selected_id_df.loc[:,'ms_sample_id'] = (selected_id_df.loc[:,'ms_sample_id']
.str.strip()
.str.replace("frozen_2w", 'frozen-2w')
.str.replace("RT_2w", 'RT-2w')
.str.replace('mitra_baseline', 'mitra-baseline')
.str.replace('LT_RT', 'LT-RT')
.str.replace('-1', '_1')
.str.replace('-2', '_2')
.str.replace('-3', '_3')
.str.replace('-4', '_4'))
运行上述声明后,我再次使用
selected_id_df.ms_sample_id.unique()
这一次产生了:
array(['mitra-baseline_310808_1', 'mitra-baseline_310808_2',
'mitra-baseline_310808_3', 'mitra-baseline_310808_4',
'mitra-baseline_310907_1', 'mitra-baseline_310907_2',
'mitra-baseline_310907_3', 'mitra-baseline_310907_4',
'mitra-baseline_311090_1', 'mitra-baseline_311090_2',
'mitra-baseline_311090_3', 'mitra-baseline_311090_4',
'mitra-baseline_311091_1', 'mitra-baseline_311091_2',
'mitra-baseline_311091_3', 'mitra-baseline_311091_4',
'mitra-baseline_311123_1', 'mitra-baseline_311123_2',
'mitra-baseline_311123_3', 'mitra-baseline_311123_4',
'frozen_2w_310808_1', 'frozen_2w_310808_2', 'frozen_2w_310907_1',
'frozen_2w_310907_2', 'frozen_2w_311090_1', 'frozen_2w_311090_2',
'frozen_2w_311091_1', 'frozen_2w_311091_2', 'frozen_2w_311123_1',
'frozen_2w_311123_2', 'RT_2w_310808_1', 'RT_2w_310808_2',
'RT_2w_310907_1', 'RT_2w_310907_2', 'RT_2w_311090_1',
'RT_2w_311090_2', 'RT_2w_311091_1', 'RT_2w_311091_2',
'RT_2w_311123_1', 'RT_2w_311123_2', 'LT-RT_310808_1',
'LT-RT_310808_2', 'LT-RT_310907_1', 'LT-RT_310907_2',
'LT-RT_311090_1', 'LT-RT_311090_2', 'LT-RT_311091_1',
'LT-RT_311091_2', 'LT-RT_311123_1', 'LT-RT_311123_2',
'LT-frozen_310808_1', 'LT-frozen_310808_2', 'LT-frozen_310907_1',
'LT-frozen_310907_2', 'LT-frozen_311090_1', 'LT-frozen_311090_2',
'LT-frozen_311091_1', 'LT-frozen_311091_2', 'LT-frozen_311123_1',
'LT-frozen_311123_2'], dtype=object)
我们可以看到我的替换声明对我的2个替换不起作用:
.str.replace("frozen_2w", 'frozen-2w')
.str.replace("RT_2w", 'RT-2w')
我非常困惑,为什么会这样?
谢谢
答案 0 :(得分:1)
试试这个正则表达式解决方案:
s.replace('-(?=[0-9])','_', regex=True).replace('_(?=2w|baseline|RT)','-', regex=True)
说明:
-
之前找到[0-9]
并替换为_
_
之前找到2w or baseline or RT
并替换为-
为例:
import pandas as pd
s = pd.Series(['mitra_baseline_310808-1', 'mitra_baseline_310808-2',
'mitra_baseline_310808-3', 'mitra_baseline_310808-4',
'mitra_baseline_310907-1', 'mitra_baseline_310907-2',
'mitra_baseline_310907-3', 'mitra_baseline_310907-4',
'mitra_baseline_311090-1', 'mitra_baseline_311090-2',
'mitra_baseline_311090-3', 'mitra_baseline_311090-4',
'mitra_baseline_311091-1', 'mitra_baseline_311091-2',
'mitra_baseline_311091-3', 'mitra_baseline_311091-4',
'mitra_baseline_311123-1', 'mitra_baseline_311123-2',
'mitra_baseline_311123-3', 'mitra_baseline_311123-4',
'frozen_2w_310808-1', 'frozen-2w_310808-2', 'frozen-2w_310907-1',
'frozen-2w_310907-2', 'frozen-2w_311090-1', 'frozen-2w_311090-2',
'frozen-2w_311091-1', 'frozen-2w_311091-2', 'frozen-2w_311123-1',
'frozen-2w_311123-2', 'RT-2w_310808-1', 'RT-2w_310808-2',
'RT-2w_310907-1', 'RT-2w_310907-2', 'RT-2w_311090-1',
'RT-2w_311090-2', 'RT-2w_311091-1', 'RT-2w_311091-2',
'RT-2w_311123-1', 'RT-2w_311123-2', 'LT_RT_310808_1',
'LT_RT_310808_2', 'LT_RT_310907_1', 'LT_RT_310907_2',
'LT_RT_311090_1', 'LT_RT_311090_2', 'LT_RT_311091_1',
'LT_RT_311091_2', 'LT_RT_311123_1', 'LT_RT_311123_2',
'LT-frozen_310808_1', 'LT-frozen_310808_2', 'LT-frozen_310907_1',
'LT-frozen_310907_2', 'LT-frozen_311090_1', 'LT-frozen_311090_2',
'LT-frozen_311091_1', 'LT-frozen_311091_2', 'LT-frozen_311123_1',
'LT-frozen_311123_2'])
print(s.replace('-(?=[0-9])','_', regex=True).replace('_(?=2w|baseline|RT)','-', regex=True))