How do I subtract values in a csv file that meet certain criteria with loop in python?

128 Views Asked by At

I am tryig to subtract the surp value in a csv file that meets certain criteria by another surp value that meets another criteria which I have to repeat 60 times as follows:

surp value of [prime type == 'nl_unamb' or 'nl_amb', target nr == '0', prime nr == '0'] - surp value of [prime type == 'en_unamb' or 'en_amb', target nr = '0', prime nr = '0'] = value 1

...

surp value of [prime type == 'nl_unamb' or 'nl_amb', target nr == '59', prime nr == '59'] - surp value of [prime type == 'en_unamb' or 'en_amb', target nr = '59', prime nr = '59'] = value 60

For this end, I tried the following code:

import pandas as pd

df = pd.read_csv("surprisal.primed_extracted.csv")

for i in range(60):

    pr_nl = df.loc[(df['surp'])
                  & (df['prime_type'] == 'nl_unamb')
                  & (df['prime_type'] == 'nl_amb')
                  & (df['target_nr'] == i)
                  & (df['prime_nr'] == i)]

    pr_en = df.loc[(df['surp'])
                  & (df['prime_type'] == 'en_unamb')
                  & (df['prime_type'] == 'en_amb')
                  & (df['target_nr'] == i)
                  & (df['prime_nr'] == i)]
    
    df.['sub_prime_language'] = pr_nl.loc['surp'] - pr_en.loc['surp']

However, I get this following error:

Traceback (most recent call last):
  File "calculate.py", line 13, in <module>
    & (df['prime_nr'] == i)]
  File "/usr/lib/python2.7/dist-packages/pandas/core/ops.py", line 786, in wrapper
    return filler(self._constructor(na_op(self.values, other.values),
  File "/usr/lib/python2.7/dist-packages/pandas/core/ops.py", line 758, in na_op
    result = lib.vec_binop(x, y, op)
  File "pandas/lib.pyx", line 914, in pandas.lib.vec_binop (pandas/lib.c:16248)
  File "pandas/lib.pyx", line 907, in pandas.lib.vec_binop (pandas/lib.c:16122)
TypeError: unsupported operand type(s) for &: 'float' and 'bool'

I am looking forward to hearing any solutions anyone could come up with!

Kind regards,
Mijin

0

There are 0 best solutions below