Search code examples
pythonpandasstringdataframesubstring

Pandas: Create new column with substring from other column


I have various columns which contains values like this:

FSA: 123.45 US DOLLAR 
FSA: 67.89 JAPANESE YEN

I would like to create a new column that contains this:

123.45
67.89

The 'FSA: ' is a constant value, but the currency type and amount will vary. What is an efficient method for achieving this without having to go row by row?


Solution

  • You can use str.extract:

    >>> df['Col'].str.extract(r'(\d+\.?\d*)').astype(float)
          Col
    0  123.45
    1   67.89
    

    Input:

    >>> df
                           Col
    0    FSA: 123.45 US DOLLAR 
    1  FSA: 67.89 JAPANESE YEN