Search code examples
pythonpandasdataframejupyter-notebookmultiplication

How to multiply first two digit in a single dataframe column in pandas?


I have tried many ways in doing this but it doesn't work for my case. Many of them are multiplied by columns, for my case is need to get the first two digit from a single column and multiply.

this is a column in a dataset and I need to get the first two-digit and multiply with each other For example: for the first row I need to get 4 multiply by 5 and the result will store in a new column

May I know how to do it?

Thank you in advanced^^


Solution

  • For following dataframe

    import pandas as pd
    d={'locationID':[12,234,34]}
    data=pd.DataFrame(data=d)
    
    print(data)
       locationID
    0    12
    1   234
    2    34
    

    If you want to multiply all the digits

    function to multiply all the digits,

    def multiplyall(number):
      result=1
      for i in range(len(str(number))):
        result=int(str(number)[i])*result    
      return result
    

    create column and add values according to the function in one line with insert(location, column name, column values)

    data.insert(len(data.columns), 'new_column', data['locationID'].apply(lambda x: multiply_all(x)).tolist())
    

    you'll get the following output

    print(data)
       locationID  new_column
    0      12           2
    1     234          24
    2      34          12
    

    If you want to multiply ONLY 1st and 2nd digits

    function to multiply 1st and 2nd digits,

    def multiply_firstsecond(number):
      result=number
      if len(str(number))>1:
        result=int(str(number)[0])* int(str(number)[1])
      return result
    

    Similarly,

    data.insert(len(data.columns), 'new_column', data['locationID'].apply(lambda x: multiply_firstsecond(x)).tolist())
    

    output for this one,

    print(data)
       locationID  new_column
    0      12           2
    1     234           6
    2      34          12
    

    PLEASE make sure you have no NaN or non-numeric values in the column to avoid errors.