0
$\begingroup$

enter preformatted text hereHere I have data to import from a CSV file.I wrote an equation inside the class and to solve the equation data will import from the CSV file. When I run my code I got an error like "tuple indices must be integers or slices, not str" using panda python. Can anyone help me to solve this problem? I upload my code and CSV file here.

def ph_convert(time,we,h,a,w):
while time <= 30:
    level = 1.1
    level = float(level)
    if w == 1:
        ph= ((((6*we)+(1*h))/level -(4*a)))/time
    else:
        ph= ((6+((1*we)+(3*h))/level -(6 *a)))/time  
    break
while time <=60:
    level = 1.25
    level = float(level)
    if w == 1:
        ph= ((((6*we)+(1*h))/level -(4*a)))/time
    else:
        ph= ((6+((1*we)+(3*h))/level -(6 *a)))/time  
    break
print(ph)

data = pd.read_csv('data1.csv')
data['time'] = data['time'].apply(time_convert)
we = data['we'].astype(float)
h = data['h'].astype(float)
a = data['a'].astype(float)
w = data['w'].astype(float)
time = data['time'].astype(float)
print(ph_convert(time,we,h,a,w))

Subset of my CSV file:

we  h   a   w   time
48.1    150 53  1   6:15:00
48.1    150 53  1   9:00:00
48.1    150 53  1   9:25:00
48.1    150 53  1   9:30:00
48.1    150 53  1   11:00:00

error:

enter image description here

[![enter image description here][2]][2]
$\endgroup$
2
$\begingroup$
while time <= 30:

You are trying to evaluate the vector time, but in python, boolean evaluations with that expression can only be done over scalars. The thing is, as time has multiple values, the Python interpreter can't figure out if the condition must hold over all of them, just one, etc.

If you want to evaluate if any of the values of time, or all the values in time you can use methods like time.any() or time.all()

You can always walk through all the items in time and perform the evaluation on them:

for t in time:
    if t <= 30:
        ...
| improve this answer | |
$\endgroup$
  • $\begingroup$ ohh I will try to change it. There is another problem. I have another csv that same time with ph column. I want to apply to this ph column what I'm getting the values from my formula. I will upload my second csv. So then if I want to write use this time column mean data['time].iloc[2]-data['time'].iloc[0] = 15min or 60 min then activity level apply to the formula. I don't know how to write it properly . Can you give me any suggestion to solve this? $\endgroup$ – awa Dec 3 '18 at 17:12
  • $\begingroup$ Please consider up voting or accepting the answer if it has been of help. For the second question, it is worth opening another one $\endgroup$ – ignatius Dec 3 '18 at 17:14
  • $\begingroup$ sure. done. I commented it. $\endgroup$ – awa Dec 3 '18 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.