0

I am trying to read a CSV file and make some calculations based on the values in the file. The CSV file is structured like so:

data, 1666.00 +- 40.82
Higgs, 0.83 +- 0.70
MultiBoson, 4.34 +- 0.90
Top, 9.17 +- 1.32
Wjets, 56.59 +- 15.92
Zjets, 29.08 +- 14.38

This is what I tried to do:

import pandas as pd
import numpy as np
def getvalue(df,name):
    data = df['point'].str.contains(name)
    data_v = df[data]['mainvalue']
    data_e = df[data]['error']
    print(data_v,data_e)
    return [data_v,data_e]

def getQCD(file="yiled.csv"):
    df = pd.read_csv('yield.csv',header=None,sep=' ',names=["point",'mainvalue','sign','error'])
    data1 = getvalue(df,"data")
    higgs = getvalue(df,"Higgs")
    zjets = getvalue(df,"Zjets")
    wjets = getvalue(df,"Wjets")
    top = getvalue(df,"Top")
    multi_boson = getvalue(df,"MultiBoson")
    QCD_v = data1[0] - higgs[0] - zjets[0] - wjets[0] - top[0] - multi_boson[0]
    QCD_e = np.sqrt(data1[0] + pow(higgs[1],2)+ pow(zjets[1],2) + pow(wjets[1],2) + pow(top[1],2) + pow(multi_boson[1],2))
    print("QCD: ",QCD_v," ",QCD_e)

getQCD()

AttributeError: 'float' object has no attribute 'sqrt'

Could you give me a hand?

gmdev
  • 2,725
  • 2
  • 13
  • 28
xin wang
  • 9
  • 5
  • Please show expected output. Please see: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – David Erickson Dec 22 '20 at 02:49
  • In this line of code `data = df['point'].str.contains(name)` you are creating a boolean series of ONE column of `True` / `False`. You are then trying to access the other columns from this series? I spot a couple of different issues in your code, so please just post desired output. You will probably want to use `np.select` to conditionally make changes, which would simplify your code a lot. No need to create custom functions. Use existing pandas methods and numpy. – David Erickson Dec 22 '20 at 02:53
  • 1
    always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information. – furas Dec 22 '20 at 02:54
  • 1
    error shows problem with `sqrt` which you have only in `np.sqrt` and it can means you assigned `float` value to `np` - ie. `np = 3.14` - and now `np.sqrt` means `(3.14).sqrt` and this is why you get message `'float' object has no attribute 'sqrt'`. Try in Python `(3.14).sqrt` and you get the same error message. – furas Dec 22 '20 at 02:56

0 Answers0