lundi 13 mai 2019

Comparing two columns and if condition is met add '1' to a new column

I cannot figure out how to compare two columns and if one columns is greater than or equal to another number input '1' to a new column. If the condition is not met I would like python to do nothing.

The data set for testing is here:

data = [[12,10],[15,10],[8,5],[4,5],[15,'NA'],[5,'NA'],[10,10], [9,10]]
df = pd.DataFrame(data, columns = ['Score', 'Benchmark'])

   Score Benchmark
0     12        10
1     15        10
2      8         5
3      4         5
4     15        NA
5      5        NA
6     10        10
7      9        10

The desired output is:

desired_output_data = [[12,10, 1],[15,10,1],[8,5,1],[4,5],[15,'NA'],[5,'NA'],[10,10,1], [9,10]]
desired_output_df = pd.DataFrame(desired_output_data, columns = ['Score', 'Benchmark', 'MetBench'])

   Score Benchmark  MetBench
0     12        10       1.0
1     15        10       1.0
2      8         5       1.0
3      4         5       NaN
4     15        NA       NaN
5      5        NA       NaN
6     10        10       1.0
7      9        10       NaN

I tried doing something like this:

if df['Score'] >= df['Benchmark']:
    df['MetBench'] = 1

I am new to programming in general so any guidance would be greatly appreciated. Thank you!

Aucun commentaire:

Enregistrer un commentaire