This code isn't working in Jupyter IDE. I can't find my mistake. Please help.
The first 5 rows of the dataframe I am using is shown here:
Try the following:
df.loc[df['compound'] > 0,'SentimentType'] = 'Positive'
df.loc[df['compound'] < 0,'SentimentType'] = 'Negative'
df.loc[df['compound'] == 0,'SentimentType'] = 'Neutral'
Instead of retrieving the column through df.compound, you should do df['compound']. You can also tell from the error message that you received that df.compound is a method name and not the column that you are looking for.
If you are trying to compare values of 'compound' column, you must use df['compound'] instead of df.compound, which is a method.
Maybe the following code can help you:
Function to classify 'Sentiment_Type'
def sentiment(score):
if score < 0:
return "Negative"
elif score > 0:
return "Positive"
else:
return "Neutral"
After, you can use this function to create a new column
df['Sentiment_Type'] = df['compound'].apply(sentiment)
add brackets to 1 st line df.compund() >0
Related
I am trying to iterate through dataframe rows and set a new column to either a 1 or 0 depending on conditions. Up to the if statement works fine, but once the elif section is added it gives me an "index out of bounds error". Any ideas on how to remedy this?
low=history.low
high = history.high
history['bottom'] = " "
history['top']=" "
for i in range(len(history)):
if (low[i] < low[i-1]) and (low[i] < low[i-2]) and (low[i] < low[i+1]) and (low[i] < low[i+2]) :
history['bottom'][i] = 1
elif (high[i] > high[i-1]) and (high[i] > high[i-2]) and (high[i] > high[i+1]) and (high[i] > high[i+2]):
history['top'][i]=1
else:
history['top'][i] = 0
history['bottom'][i] = 0
One of our error is explained by #Code-Apprentice
Other which I found and I think you are looking for this is that in lines
history['bottom'][i] = 1 and history['top'][i]=1 you are trying to change the value of an index which may not be present in it.
For example if i = 1 then the lines specified above will generate error as index 1 is not present in them. (They only have index 0).
Instead of using index to change values you can use .append to add values
It's because this is trying to access an index that doesn't exist. I would like to see more of the code above to know what history.low and history.high is referring to for the value.
But have you gotten any results before the error?
Also, please explain len(history). In your code there is a history dictionary where you have history['bottom'] = " " and history['top']=" ", but at the same time you have low=history.low and high = history.high. What's the difference between these two history objects/variables?
Please show more of your code.
So I'm trying to go through my dataframe in pandas and if the value of two columns is equal to something, then I change a value in that location, here is a simplified version of the loop I've been using (I changed the values of the if/else function because the original used regex and stuff and was quite complicated):
pro_cr = ["IgA", "IgG", "IgE"] # CR's considered productive
rows_changed = 0
prod_to_unk = 0
unk_to_prod = 0
changed_ids = []
for index in df_sample.index:
if num=1 and color="red":
pass
elif num=2 and color="blue":
prod_to_unk += 1
changed_ids.append(df_sample.loc[index, "Sequence ID"])
df_sample.at[index, "Functionality"] = "unknown"
rows_changed += 1
elif num=3 and color="green":
unk_to_prod += 1
changed_ids.append(df_sample.loc[index, "Sequence ID"])
df_sample.at[index, "Functionality"] = "productive"
rows_changed += 1
else:
pass
print("Number of productive columns changed to unknown: {}".format(prod_to_unk))
print("Number of unknown columns changed to productive: {}".format(unk_to_prod))
print("Total number of rows changed: {}".format(rows_changed))
So the main problem is the changing code:
df_sample.at[index, "Functionality"] = "unknown" # or productive
If I run this code without these lines of code, it works properly, it finds all the correct locations, tells me how many were changed and what their ID's are, which I can use to validate with the CSV file.
If I use df_sample["Functionality"][index] = "unknown" # or productive the code runs, but checking the rows that have been changed shows that they were not changed at all.
When I use df.at[row, column] = value I get "AttributeError: 'BlockManager' object has no attribute 'T'"
I have no idea why this is showing up. There are no duplicate columns. Hope this was clear (if not let me know and I'll try to clarify it). Thanks!
To be honest, I've never used df.at - but try using df.loc instead:
df_sample.loc[index, "Functionality"] = "unknown"
You can also iat.
Example: df.iat[iTH row, jTH column]
I've been trying to make a easy Object Oriented game for console. Initially, this game gonna create a list that created by the random numbers between 0 and 9. Length of that list is gonna be 5 but it sometimes can be 4 because if list contains 0 more than one it's deleting automatically. After that this is gonna put 0 to a index after some calculations for where to insert 0. Program is gonna get the first index of the variable that will be calculated for where to put 0 but sometimes first index might be more than my length of list. For this, I created a variable named IndexOfZero and I have setted this equal to first index of the variable that calculated where to put 0.
After I've done that I'm getting this error called TypeError: 'int' object has no attribute 'getitem' How can i fix this?
def PutZero(self, x='t'):
elements = 0
element = []
putZero = 0
for x in range(len(self.list)):
elements += self.list[x]
element.append(elements)
sum = element[-1]
print(sum,'This is the sum.')
if self.list.count(0) == 0:
putZero = (math.sqrt(math.pow(random.randrange(50,100), 5) / sum + self.list[2]) )
putZero = int(math.floor(putZero))
IndexOfZero = putZero[0] """the line that I'm getting error"""
if IndexOfZero == 0:
IndexOfZero = random.randrange(0,3)
print(IndexOfZero,'Randomly generated value')
if IndexOfZero > 5 or IndexOfZero > 4:"""Do I still get the same error for this line and for the below ?"""
IndexOfZero = random.randrange(0,3)
if x:
return '{}'.format(self.list)
Error is this :
Traceback (most recent call last):
File
"/home/guney/Find_Zer0/Code.py", line 52, in PutZero
IndexOfZero = putZero[0]
TypeError: 'int' object has no attribute '__getitem__'
The problem is that you can't index a number. I think you were misunderstanding the tutorial you were using, or the tutorial isn't reliable.
If you want to get the first digit of a number, you could either use math, or turn it into a string first.
Since you're using indexing, you seem to be trying to do the second way.
Here's an example of how to do this:
num = 1234
str_num = str(num)
first_char = str_num[0] # Get the first character of the string
first_digit = int(first_char) # Parse the string as an integer
print(first_digit) # Prints the number 1
This way is slower than using straight math, but in most cases that shouldn't be a problem.
I am trying to create and calculate a field called Score in an attribute table. Score would depend on two other fields, Width and Category. Width contains integer values, Category contains text. The Score field should assign a number of points based on the values of these two fields.
I was able to write a Python script assign values to SCORE based on the value of Width. However, once I tried to add Category as a parameter, I kept getting error messages saying that the parameters are not valid.
The code below worked (Width only):
expression = "getClass(float(!Width!))"
codeblock = """def getClass(wd):
if wd<=450:
return 1
elif wd>450 and wd<900:
return 2
else:
return 3"""
arcpy.AddField_management("featurelayer", "Score", "SHORT")
This code includes Category as a parameter and does not work:
expression = "getClass(!Width!,!Category!)"
codeblock = """def getClass(wd,cat):
if wd<=35:
return 1
elif wd<90 and cat=='RED':
return 2
else:
return 3"""
arcpy.AddField_management("featurelayer", "Score", "SHORT")
Thank you!
***I did try converting the two parameters in the second code - I tried float() for both, as well as str() for Category since it is a text field. Neither one worked, and the error messages were not very specific: "failed to execute getClass()"
***The Category field is completely populated (either 'RED' or 'BLUE')
Using the expression and codeblock method is necessary within ArcMap's Field Calculator, but not when writing a Python script.
Try an UpdateCursor instead. It is easier to read, and tends to be easier to debug as a result.
arcpy.AddField_management("featurelayer", "Score", "SHORT")
updateFields = ["Width", "Category", "Score"]
with arcpy.da.UpdateCursor("featurelayer", updateFields) as cursor:
for row in cursor:
if row[0] <= 35:
row[2] = 1
elif row[0] < 90 and row[1] == 'RED':
row[2] = 2
else:
row[2] = 3
cursor.updateRow(row)
The code im trying to create is to print a wavelength such as radio waves or microwaves based on the wavelength value input.
userInput = input("Enter wavelength (m) value: ")
waveValue= float(userInput)
if waveValue > 10**-1 :
print("Radio Waves")
elif waveValue < 10**-3 :
print("Microwaves")
elif waveValue < 7*10**-7 :
print("Infared")
elif waveValue <4-10**-7 :
print(" Visible light")
elif waveValue <10**-8 :
print( "Ultraviolet")
elif waveValue <10**-11 :
print( "X-rays")
elif waveValue >10**-11 :
print("Gamma rays")
else :
print()
Any hints on how I can get the second if statement to work. Every input I put in just outputs radio waves because my arguments does not work properly.
Also there is 5 more inputs that I will have to use elif commands for.
Are you trying to use powers of 10 here? Because the convention for "10 to the minus 1" for example, is 1.0e-1. What you have in your code translates to "10 times -1" or -10, which I don't think you intended.
I would rewrite as:
if waveValue > 1.0e-1:
print("Radio Waves")
elif (waveValue > 1.0e-3) and (waveValue < 1.0e-1):
print("Micro Waves")
as a prior answer pointed out, it is also more efficient to order the answers so that one side of the comparison is not needed (those values have already been accounted for by earlier tests).
for example:
if waveValue < 1.0e-3:
<not in current code>
elif waveValue < 1.0e-1:
print("Micro Waves")
else:
print("Radio Waves")
Nothing wrong with your approach for a small number of "break values". For a large number of "break values" you are better off to create a table with the break values and the corresponding action.
i.e., in your case, a table with wavelength and classification. First row containing 10e-1 and "Radio Waves", 2nd row containing 10e-3 and "Microwaves"
In code, you just loop though the until until you find the correct row for waveValue.
This is common in Tax Tables. As a bonus, you can store your tax tables in an external database instead of hard-coding the values in your program.
I think your second statement is wrong.
It says waveValue >10*-1<10*-3 which I think is >-10 and < -30 and if what I say is correct then there can't be numbers bigger than -10 that are smaller than -30
A list of tuples could work quite well here:
wave_categories = []
# Load up your data of definitions
wave_categories.append((1e-3, 'Microwaves'))
wave_categories.append((1e-2, 'Somewaves'))
wave_categories.append((1e-1, 'Radiowaves'))
wave_categories.append((1, 'Ultrawaves'))
Then your detection becomes a loop:
def find_wave(wavelength):
for category_wavelength, name in wave_categories:
if wavelength < category_wavelength:
return name
raise Exception('Wavelength {} not detected in list'.format(wavelength))
To make it work for lots and lots of categories, just add more data to wave_categories.
You can use the bisect module for this:
from bisect import bisect
freqs = [10**-11, 10**-8, 4*10**-7, 7*10**-7, 10**-3, 10**-1]
names = ['Gamma rays', 'X-rays', 'Ultraviolet', 'Visible light',
'Infared', 'Microwaves', 'Radio Waves']
>>> names[bisect(freqs, 10**-2)]
'Radio Waves'
>>> names[bisect(freqs, 10**-4)]
'Microwaves'