Python从字符串中提取以连字符分隔的数字

2024-10-01 17:28:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图提取数据帧列(名称深度)中由连字符分隔的2个数字(深度从和深度到)。虽然第一个数字提取正确,但第二个数字提取不正确。我试过很多方法

ConvCore = pd.read_csv(r'ConvCore.csv', encoding='cp1252')
ConvCore.columns = ['Depth', 'k', 'phi', 'Well']
ConvCore['DepthFrom'] = ConvCore['Depth'].str.extract('([0-9.]+)')

#ConvCore['DepthTo'] = ConvCore['Depth'].str.extract('-([0-9.]+)')
#for i in ConvCore:
    #ConvCore['DepthTo'] = re.search(r'(\d+)-', ConvCore['Depth'][i-1])
    #ConvCore['DepthFrom'] = ConvCore['Depth'].str.extract('(\d+)').astype(float)
    #DepthTo = ConvCore['Depth'].str.extract('(?P<digit1>[0123456789])').astype(float)
    #ConvCore['DepthTo'] = ConvCore['Depth'].str.split("-")
    #ConvCore['DepthFrom'] = re.match(r'(\d+)', ConvCore['Depth']).group()

enter image description here


Tags: csv数据方法re名称extract数字float
2条回答

试着这样做:

ConvCore['DepthFrom'] = ConvCore['Depth'].str.split("-", expand=True)[0]
ConvCore['DepthTo'] = ConvCore['To'].str.split("-", expand=True)[1]

您可以拆分这些值,然后将新值分配给数据帧。我使用了一个示例数据集来模拟您的场景

In [4]: df = pd.DataFrame({'num_legs': ['20-30', '40-60', '80-90', '0-10'],
    ...:
    ...:                    'num_wings': [2, 0, 0, 0],
    ...:
    ...:                    'num_specimen_seen': [10, 2, 1, 8]},
    ...:
    ...:                   index=['falcon', 'dog', 'spider', 'fish'])

In [5]: ndf = pd.DataFrame(df.num_legs.str.split('-').tolist(), columns = ['x1', 'x2'])

In [6]: df[ ndf.columns ] = ndf.values

In [7]: df
Out[7]:
       num_legs  num_wings  num_specimen_seen  x1  x2
falcon    20-30          2                 10  20  30
dog       40-60          0                  2  40  60
spider    80-90          0                  1  80  90
fish       0-10          0                  8   0  10

在你的例子中,代码应该是这样的

ndf = pd.DataFrame(ConvCore.Depth.str.split('-').tolist(), columns = ['DepthFrom', 'DepthTo'])

ConvCore[ ndf.columns ] = ndf.values

相关问题 更多 >

    热门问题