0

I have the following dataframe:

      import numpy as np
      import pandas as pd

      df1 = pd.DataFrame({'ID_df1': [1,2,3,4],
                          'Name_df1': ['John', 'Alex', 'Alan', 'Marie'],
                          'Cod_job_df1': [5, 8, 30, 40]})

For a school project, the teacher asked us to exercise function creation. So I made the following function that checks if a number is greater than 10:

      def func_test(var_number):

          if(var_number) > 10:
              return True
          else:
              return False  

I need to apply the created function on a datadrame column. So I made the following code:

      df1['Check'] = 0

      for i in range(0, len(df1)):
          if(func_test(df1['Cod_job_df1'].iloc[i]) == True):
              df1['Check'].iloc[i] = 1
          else:
              df1['Check'].iloc[i] = 0 

The result is as expected:

       ID_df1   Name_df1    Cod_job_df1  Check
         1        John          5        0
         2        Alex          8        0
         3        Alan         30        1
         4        Marie        40        1

However, I would like to find out if there is a more efficient way to use the function without having to iterate through for()? Thanks!

Jane Borges
  • 496
  • 2
  • 9

0 Answers0