AttributeError: 'float' object has no attribute 'find' - python

i want to find degree holders.
The following code is causing AttributeError: 'float' object has no attribute 'find' and I do not how to fix it:
edu = Edu_data
# Function to identify degree
def degree(x):
#if x.find('Bachelor') != -1 or x.find("Bachelor's") != -1 or x.find('BS') != -1 or x.find('bs') != -1 or x.find('Bs') != -1 or x.find('Bachelors') != -1 or x.find('Undergraduate') != -1 or x.find('graduated')!= -1 or x.find('BSE')!= -1 or x.find('Enginee') != -1 or x.find('BCS') != -1:
if x.find('Bachelor') != -1 or x.find("Bachelor's") != -1 or x.find('BS') != -1 or x.find('bs') != -1:
return(1)
if x.find('Master') != -1 or x.find("Master's") != -1 or x.find('M.S') != -1 or x.find('MS') != -1 or x.find('MPhil') != -1 or x.find('MBA') != -1 or x.find('MicroMasters') != -1 or x.find('MSc') != -1 or x.find('MSCS') !=-1 or x.find('MSDS')!=-1:
return(2)
if x.find('PhD') != -1 or x.find('P.hd') != -1 or x.find('Ph.D') != -1 or x.find('ph.d') != -1:
return(3)
else:
return(0)
# Create degree column
edu['deg'] = list(map(degree, edu['Last_degree']))
edu
The full traceback for the error:
AttributeError Traceback (most recent call last)
<ipython-input-39-9a79283a9b17> in <module>
16
17 # Create degree column
---> 18 edu['deg'] = list(map(degree, edu['Last_degree']))
19
20 edu
<ipython-input-39-9a79283a9b17> in degree(x)
5 # Function to identify degree
6 def degree(x):
----> 7 if x.find('Bachelor') != -1 or x.find("Bachelor's") != -1 or x.find('BS') != -1 or x.find('bs') != -1 or x.find('Bs') != -1 or x.find('Bachelors') != -1 or x.find('Undergraduate') != -1 or x.find('graduated')!= -1 or x.find('BSE')!= -1 or x.find('Enginee') != -1 or x.find('BCS') != -1:
8 return(1)
9 if x.find('Master') != -1 or x.find("Master's") != -1 or x.find('M.S') != -1 or x.find('MS') != -1 or x.find('MPhil') != -1 or x.find('MBA') != -1 or x.find('MicroMasters') != -1 or x.find('MSc') != -1 or x.find('MSCS') !=-1 or x.find('MSDS')!=-1:
AttributeError: 'float' object has no attribute 'find'
image of dataset

Does the image show all your data. Is it possible that somewhere in the data frame's Last_degree column contains a float?
If yes, change this line:
edu['deg'] = list(map(degree, edu['Last_degree']))
to
edu['deg'] = list(map(degree, edu['Last_degree'].astype(str)))

Related

convert tradingview script into python3

I am looking for a converting from TV to python. Just a little code. This is the code in tradingview :
last_signal = 0
long_final = longCond and (nz(last_signal[1]) == 0 or nz(last_signal[1]) == -1)
short_final = shortCond and (nz(last_signal[1]) == 0 or nz(last_signal[1]) == 1)
last_signal := long_final ? 1 : short_final ? -1 : last_signal[1]
for variable :
-> longCond and shortCond, i have the right value (I compared between plot)
But for others, i have some differences (i think, because of last_signal)
this is my code in python :
for x in range(0,len(mavi),1):
last_signal[i] = 0
if x == 0:
longCond_tmp = 0
shortCond_tmp = 0
last_signal_tmp = 0
short_final_tmp = 0
long_final_tmp = 0
else:
if ((longCond_tmp and ((last_signal[i-1])) == 0) or ((last_signal[i-1]) == -1)):
long_final_tmp = 1
else:
long_final_tmp = 0
if ((shortCond_tmp and ((last_signal[i-1])) == 0) or ((last_signal[i-1]) == 1)):
short_final_tmp = 1
else:
short_final_tmp = 0
if long_final_tmp != 0:
last_signal_tmp = 1
else:
if short_final_tmp != 0:
last_signal_tmp = -1
else:
last_signal_tmp = last_signal[i-1]
last_signal[i] += last_signal_tmp
Are there errors in my script in python ?
Ok i found.
Just the number of "(" in
if ((longCond_tmp and ((last_signal[i-1])) == 0) or ((last_signal[i-1]) == -1)):
long_final_tmp = 1
else:
long_final_tmp = 0
if ((shortCond_tmp and ((last_signal[i-1])) == 0) or ((last_signal[i-1]) == 1)):
short_final_tmp = 1
else:
short_final_tmp = 0

NameError: name 'jam' is not defined [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I followed along a tutorial at https://towardsdatascience.com/audio-to-guitar-tab-with-deep-learning-d76e12717f81 but i am getting an error.
# Initialize variables
cnt_row = -1
cnt_col = 0
cnt_zero = 0
# Grab all relevant MIDI data (available in MIDI_dat)
for i in range(0, len(jam['annotations'])):
if jam['annotations'][int(i)]['namespace'] == 'note_midi':
for j in range(0, len(sorted(jam['annotations'][int(i)]['data']))):
cnt_row = cnt_row + 1
for k in range(0, len(sorted(jam['annotations'][int(i)]['data'])[int(j)]) - 1):
if cnt_zero == 0:
MIDI_arr = np.zeros((len(sorted(jam['annotations'][int(i)]['data'])), len(sorted(jam['annotations'][int(i)]['data'])[int(j)]) - 1), dtype = np.float32)
cnt_zero = cnt_zero + 1
if cnt_zero > 0:
MIDI_arr = np.vstack((MIDI_arr, np.zeros((len(sorted(jam['annotations'][int(i)]['data'])), len(sorted(jam['annotations'][int(i)]['data'])[int(j)]) - 1), dtype = np.float32)))
cnt_zero = cnt_zero + 1 # Keep
if cnt_col > 2:
cnt_col = 0
MIDI_arr[cnt_row, cnt_col] = sorted(jam['annotations'][int(i)]['data'])[int(j)][int(k)]
cnt_col = cnt_col + 1
MIDI_dat = np.zeros((cnt_row + 1, cnt_col), dtype = np.float32)
cnt_col2 = 0
for n in range(0, cnt_row + 1):
for m in range(0, cnt_col):
if cnt_col2 > 2:
cnt_col2 = 0
MIDI_dat[n, cnt_col2] = MIDI_arr[n, cnt_col2]
cnt_col2 = cnt_col2 + 1
# Return the unique MIDI notes played (available in MIDI_val)
MIDI_dat_dur = np.copy(MIDI_dat)
for r in range(0, len(MIDI_dat[:, 0])):
MIDI_dat_dur[r, 0] = MIDI_dat[r, 0] + MIDI_dat[r, 1]
tab_1, = np.where(np.logical_and(MIDI_dat[:, 0] >= start, MIDI_dat[:, 0] <= stop))
tab_2, = np.where(np.logical_and(MIDI_dat_dur[:, 0] >= start, MIDI_dat_dur[:, 0] <= stop))
tab_3, = np.where(np.logical_and(np.logical_and(MIDI_dat[:, 0] < start, MIDI_dat_dur[:, 0] > stop), MIDI_dat[:, 1] > int(stop-start)))
if tab_1.size != 0 and tab_2.size == 0 and tab_3.size == 0:
tab_ind = tab_1
if tab_1.size == 0 and tab_2.size != 0 and tab_3.size == 0:
tab_ind = tab_2
if tab_1.size == 0 and tab_2.size == 0 and tab_3.size != 0:
tab_ind = tab_3
if tab_1.size != 0 and tab_2.size != 0 and tab_3.size == 0:
tab_ind = np.concatenate([tab_1, tab_2])
if tab_1.size != 0 and tab_2.size == 0 and tab_3.size != 0:
tab_ind = np.concatenate([tab_1, tab_3])
if tab_1.size == 0 and tab_2.size != 0 and tab_3.size != 0:
tab_ind = np.concatenate([tab_2, tab_3])
if tab_1.size != 0 and tab_2.size != 0 and tab_3.size != 0:
tab_ind = np.concatenate([tab_1, tab_2, tab_3])
if tab_1.size == 0 and tab_2.size == 0 and tab_3.size == 0:
tab_ind = []
if len(tab_ind) != 0:
MIDI_val = np.zeros((len(tab_ind), 1), dtype = np.float32)
for z in range(0, len(tab_ind)):
MIDI_val[z, 0] = int(round(MIDI_dat[tab_ind[z], 2]))
elif len(tab_ind) == 0:
MIDI_val = []
MIDI_val = np.unique(MIDI_val)
if MIDI_val.size >= 6:
MIDI_val = np.delete(MIDI_val, np.s_[6::])
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-56-070b180aefc0> in <module>
5
6 # Grab all relevant MIDI data (available in MIDI_dat)
----> 7 for i in range(0, len(jam['annotations'])):
8 if jam['annotations'][int(i)]['namespace'] == 'note_midi':
9 for j in range(0, len(sorted(jam['annotations'][int(i)]['data']))):
NameError: name 'jam' is not defined
Now i'm kinda new to coding, so i'm not quite sure what jam is referring to, but since i was following a tutorial, i really don't understand why it isn't defined? Thank you in advance for helping a beginner out.
you need to declare a variable jam and assign it a value from where ever you are getting it, python will not allow declaration in this manner.
cnt_row = -1
cnt_col = 0
cnt_zero = 0
jam = MIDI_dat <------- (or the variable from which you are accessing the data from)
# Grab all relevant MIDI data (available in MIDI_dat)
for i in range(0, len(jam['annotations'])):
if jam['annotations'][int(i)]['namespace'] == 'note_midi':
for j in range(0, len(sorted(jam['annotations'][int(i)]['data']))):
cnt_row = cnt_row + 1
for k in range(0, len(sorted(jam['annotations'][int(i)]['data'])[int(j)]) - 1):
if cnt_zero == 0:
MIDI_arr = np.zeros((len(sorted(jam['annotations'][int(i)]['data'])), len(sorted(jam['annotations'][int(i)]['data'])[int(j)]) - 1), dtype = np.float32)
cnt_zero = cnt_zero + 1
if cnt_zero > 0:
MIDI_arr = np.vstack((MIDI_arr, np.zeros((len(sorted(jam['annotations'][int(i)]['data'])), len(sorted(jam['annotations'][int(i)]['data'])[int(j)]) - 1), dtype = np.float32)))
cnt_zero = cnt_zero + 1 # Keep
if cnt_col > 2:
cnt_col = 0
MIDI_arr[cnt_row, cnt_col] = sorted(jam['annotations'][int(i)]['data'])[int(j)][int(k)]
cnt_col = cnt_col + 1

All cells in Conway's game of life are alive

I tried to implement Conway's game of life in Python-3.6 with this code:
import numpy as np
import time
screen = np.zeros((25,25),dtype=np.int)
while True:
print(screen)
command = input('command?')
if command[0] == 'x':
command = command.split(' ')
x = int(command[0][1:])
y = int(command[1][1:])
if screen[x][y] == 0:
screen[x][y] = 1
else:
screen[x][y] = 0
elif command == 'start':
break
while True:
for x in range(len(screen)):
for y in range(len(screen[x])):
neighbors = 0
if x != len(screen)-1:
neighbors += screen[x+1][y]
if x != 0:
neighbors += screen[x-1][y]
if y != len(screen[x])-1:
neighbors += screen[x][y+1]
if y != 0:
neighbors += screen[x][y-1]
if x != len(screen)-1 and y != len(screen[x])-1:
neighbors += screen[x+1][y+1]
if x != 0 and y != 0:
neighbors += screen[x-1][y-1]
if 0 and y != len(screen[x])-1:
neighbors += screen[x-1][y+1]
if x != len(screen)-1 and y != 0:
neighbors += screen[x+1][y-1]
if screen[x][y] == 0 and neighbors == 3:
screen[x][y] = 1
elif screen[x][y] == 1 and neighbors < 2:
screen[x][y] == 0
elif screen[x][y] == 1 and neighbors > 3:
screen[x][y] == 0
elif screen[x][y] == 1 and neighbors == 2 or 3:
screen[x][y] = 1
print(screen)
time.sleep(0.1)
The Problem is that when I try to run this code with any figure, all the cells are immediately set to 1 in the first generation and won't die off.
Can someone tell me were the issue in my code is and how to solve it?
Your problem seems to be here:
elif screen[x][y] == 1 and neighbors == 2 or 3:
You can't do that (well, you can, but it doesn't do what you expect). Instead try:
elif screen[x][y] == 1 and neighbors in (2 , 3):
(or in {2, 3}).
Check this question for more information.

Pandas less than or equal to giving TypeError: invalid type comparison

I have 4 lists based on what I want to continuously filter my Pandas data-frame
categoryList=['Parameter1', 'Parameter1', 'Parameter2', 'Parameter2']
conditionList=['b1', 'b41', 'm1', 'm2']
conditionDescList=['>', 'btn', '<=', 'btn']
conditionParamList=['1000', '2:3', '0.5', '0.1:0.3']
Now I am trying below code to filter rows from my 2 data-frames(df_custid_marker,df_custid_bp) based on categoryList
k =0
for i in conditionDescList:
if(categoryList[k]=='Parameter1'):
if(i=='btn'):
arrValues=conditionParamList[k].split(":")
minVal=arrValues[0]
maxVal=arrValues[1]
df_custid_marker=df_custid_marker[(df_custid_marker[conditionList[k]] > minVal) & (df_custid_marker[conditionList[k]] < maxVal)]
elif(i=='>'):
df_custid_marker=df_custid_marker[(df_custid_marker[conditionList[k]] > conditionParamList[k])]
elif(i=='<'):
df_custid_marker=df_custid_marker[(df_custid_marker[conditionList[k]] < conditionParamList[k])]
elif(i=='<='):
df_custid_marker=df_custid_marker[(df_custid_marker[conditionList[k]] < conditionParamList[k]) | (df_custid_marker[conditionList[k]] == conditionParamList[k])]
elif(i=='>='):
df_custid_marker=df_custid_marker[(df_custid_marker[conditionList[k]] > conditionParamList[k]) | (df_custid_marker[conditionList[k]] == conditionParamList[k])]
else:
df_custid_marker=df_custid_marker[(df_custid_marker[conditionList[k]] == conditionParamList[k])]
k+=1
k =0
for i in conditionDescList:
if(categoryList[k]=='Parameter2'):
if(i=='btn'):
arrValues=conditionParamList[k].split(":")
minVal=arrValues[0]
maxVal=arrValues[1]
df_custid_bp=df_custid_bp[(df_custid_bp[conditionList[k]] > minVal) & (df_custid_bp[conditionList[k]] < maxVal)]
elif(i=='>'):
df_custid_bp=df_custid_bp[(df_custid_bp[conditionList[k]] > conditionParamList[k])]
elif(i=='<'):
df_custid_bp=df_custid_bp[(df_custid_bp[conditionList[k]] < conditionParamList[k])]
elif(i=='<='):
df_custid_bp=df_custid_bp[(df_custid_bp[conditionList[k]] < conditionParamList[k]) | (df_custid_bp[conditionList[k]] == conditionParamList[k])]
elif(i=='>='):
df_custid_bp=df_custid_bp[(df_custid_bp[conditionList[k]] > conditionParamList[k]) | (df_custid_bp[conditionList[k]] == conditionParamList[k])]
else:
df_custid_bp=df_custid_bp[(df_custid_bp[conditionList[k]] == conditionParamList[k])]
k+=1
Now I am getting below error for <= df_custid_marker=df_custid_marker[(df_custid_marker[conditionList[k]] < conditionParamList[k]) | (df_custid_marker[conditionList[k]] == conditionParamList[k])]
raise TypeError("invalid type comparison")
The problem was data-frame column type was float and list is string

How to handle functions call in RPN

I'm having a lot of trouble converting infix notation to postfix.
For instance, I want to convert this
test(a(b+c), d()) - 3
into this
b c + a , d test 3 -
I tried this solution,
def composition(s):
i = 0
rpnstack = []
stack = []
ret = []
count = 0
while i < len(s) :
if i + 1 < len(s) and s[i + 1] == "(":
stack.append([count, rpnstack, s[i]])
i += 2
count = 1
rpnstack = []
elif s[i] == "(":
count += 1
rpnstack.append(s[i])
i += 1
elif s[i] == ")":
count -= 1
if count == 0:
for a in rpn(rpnstack):
ret.append(a)
a = stack.pop()
count = a[0]
rpnstack = a[1]
ret.append(a[2])
else:
rpnstack.append(s[i])
i += 1
else:
rpnstack.append(s[i])
i += 1
for a in rpn(rpnstack):
ret.append(a)
return ret
where RPN is the standard algorithm for the reverse polish notation and is is the infix string splitted with this regex
(\+|\-|\*|\/|\>|\<|\(|\)|\,)
But it only works sometimes.
This is the full implementation of the rpn function
operator = -10
operand = -20
leftparentheses = -30
rightparentheses = -40
empty = -50
operands = ["+", "-", "*", "/", ">", "<", "=", ","]
def precedence(s):
if s is '(':
return 0
elif s is '+' or '-':
return 1
elif s is '*' or '/' or '%':
return 2
else:
return 99
def typeof(s):
if s is '(':
return leftparentheses
elif s is ')':
return rightparentheses
elif s in operands:
return operator
elif s is ' ':
return empty
else :
return operand
def rpn(infix):
postfix = []
temp = []
for i in infix :
type = typeof(i)
if type is leftparentheses :
temp.append(i)
elif type is rightparentheses :
next = temp.pop()
while next is not '(' or skip > 0:
postfix.append(next)
next = temp.pop()
elif type is operand:
postfix.append(i)
elif type is operator:
p = precedence(i)
while len(temp) is not 0 and p <= precedence(temp[-1]) :
postfix.append(temp.pop())
temp.append(i)
elif type is empty:
continue
while len(temp) > 0 :
postfix.append(temp.pop())
return postfix
if i try to use the code against this infix expression:
i < test.func()
i get:
[' test.func', 'i ', '<']
and against this
i < 10
i get:
['i ', ' 10', '<']
How can I fix this?

Categories