Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I need to look up a value in a csv file given a criteria in a function. When I run my program I get all the values but not the one associated with my entry. Any help will be appreciated.
The date looks something like this:
rose,7.95
lily,3.95
begonia,5.95
The function I created is:
def problem3_8(csv_pricefile, flower):
import csv
archivo = open(csv_pricefile)
for row in csv.reader(archivo):
if flower in row[0]:
print(row[1])
archivo.close()
When I ran the program using the next line:
problem3_7("flowers.csv","rose")
I get all the value in the file, like this:
7.95
3.95
5.95
But the answer should be just the value associated with the second entry.
7.95
Thanks
I ran your code given and had the correct output of 7.95.
Is it possible you called the wrong function? In your question you referred to the function problem3_7 instead of the function problem3_8
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I would like to create a function using a for loop that totals up a list of numbers. I know there are some inbuilt functions in python for this but would like to use something like below.
I can get this to run without def total_numbers(number_set): but not with it.
Why not?
number_set = [1, 12, 23, 16]
def total_numbers(number_set):
total = 0
for number in number_set:
total += number
print(total)
Thanks :-)
With def you define a method, you create and only create it, you don't use it (call it)
To call it, take its name put parenthesis and fill the parameters
def total_numbers(number_set):
total = 0
for number in number_set:
total += number
print(total)
values = [1, 12, 23, 16]
total_numbers(values)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to run this code below:
namelist =[[('mixamorig:Head', 'head')],
[('mixamorig:Neck', 'neck_01')]]
for name, newname in namelist:
pb = obj.pose.bones.get(name)
if pb is None:
continue
pb.name = newname
But it gives this error:
for name, newname in namelist:
ValueError: not enough values to unpack (expected 2, got 1)
I've tried to replace the [ with ( or add different enclosures but nothing worked, how can I fix this problem?
This is because you have a list of lists of tuples, not a list of tuples. So you'd need something like:
for item in namelist:
for name, newname in item:
# do something
(or change your data as written in the comment above)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
Problem Statement
I have the CSV data as shown in the image. From this, I have to use only keep RegionName, State and the quarterly mean values from 2000 - 2016. Also, I want to use multi-indexing with [State, RegionName].
I am working on a CSV file with pandas in python. As shown in the screenshot.
Thank you in advance.
Right before the troublesome for year in range(...) loop, you did:
house_data.columns = pd.to_datetime(house_data.columns).to_period('M')
That means your columns are no longer strings. So inside the for loop:
house_data[str(year)+'q2'] = house_data[[str(year)+'-04',...]].mean(axis=1)
would fail and throw that error since there are no column with string name. To fix this, do this instead:
house_data.columns = pd.to_datetime(house_data.columns).to_period('M').strftime('%Y-%m')
However, you are better do:
house_data.columns = pd.to_datetime(house_data.columns).to_period('Q')
house_data.groupby(level=0, axis=1).mean()
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
For the first time, I'm unable to use the .shift() method on a column of the DataFrame I'm working with, giving me a DataFrame' object is not callable error.
sdf = quandl.get("AAII/AAII_SENTIMENT", authtoken="mytoken")
sdf = pd.DataFrame(data = sdf)
sdf = sdf.infer_objects()
sdf.index = pd.to_datetime(sdf.index, dayfirst=True)
sdf = sdf.iloc[9:,]
sdf['sp500_2w_future_close'] = sdf(['S&P 500 Weekly Close']).shift(-2)
I was expecting to get a new column displaying S&P 500 Weekly Close two rows down and instead got this weird error. He, p please!
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I seem to have a line of code with an object not subscribable fault but all items in the equate check out individually. The equation that gives me the problem is this
oRetVal['Assay'] = tRS2[oCSNo_dx[aRow[oFields_dx['CycleRef']]][oFields_dx['Assay']]]
tRS2 = a recordset returned from an sqlite database as a tuple
oCSNo_dx = an object used as an index
aRow = an array of values representing a single row from a db table
oFields_dx = another object used as an index
On the lines immediately prior to this error line I run the following tests.
print(oCSNo_dx[aRow[oFields_dx['CycleRef']]]) # get 1
print(oFields_dx['Assay']) # get 21
print(tRS2[1][21]) # get 0.0
print(tRS2[1]) # get single recordset (correct one) as expected
print(tRS2[oCSNo_dx[aRow[oFields_dx['CycleRef']]]][oFields_dx['Assay']]) # get 0.0
oRetVal['Assay']=0.0 # No Fault
oRetVal['Assay']=tRS2[1][21] # No fault
print(oRetVal['Assay']) # get 0.0
From the tests oRetVal['Assay'] should take the value 0.0 from tRS2[1][21] in the full equation but it gives me the error. Just to be sure I ran the following and no error was raised ..
s = oCSNo_dx[aRow[oFields_dx['CycleRef']]]
p = oFields_dx['Assay']
oRetVal['Assay']=tRS2[s][p]
I have gone with this for now but I would dearly like to know why I couldn't do the one liner in case this is a syntax thing and I come across it again. Idea's anyone?
Your square brackets are in the wrong place. You're not looking up tRS2[1][21], but tRS2[1[21]], hence the error. It should be:
tRS2[oCSNo_dx[aRow[oFields_dx['CycleRef']]]][oFields_dx['Assay']]
But really, don't do it like this; it's impossible to debug, as you have found. Set some intermediate variables and use them in the lookup.