I've got this table:
A DataFrame table which is made by using Jupyter Notebook.
This is actually only part of the table.
The complete table is actually a .csv file, and by using .head() function, only the first five rows are shown.
I need to write a function that returns and prints the maximum value, out of all the values in the second column, which its label is 'Gold'.
That function should return a single string value.
I looked up at several sources before writing my question, trying many ways to solve my problem.
It seems to be a very easy solution, but unfortunately I didn't succeed to find it.
(Are there maybe several optional solutions to this query...?)
Please help me, I'm totally confused.
Thanks!
Here are all the sources:
And here are all the ways I've tried to solve the problem, some had syntax errors:
1.a: The traditional algorithm to find out the maximum value, like in C language: a 'for' loop.
def answer_one():
row=1
max_gold = df['Gold'].row # Setting the initial maximum.
for col in df.columns:
if col[:2]=='Gold': # finding the column.
# now iterating through all the rows, finding finally the absolute maximum:
for row in df.itertuples(): # I also tried: for row=2 in df.rows:
if(df['Gold'].row > max_gold) # I also tried: if(row.Gold > max_gold)
max_gold = df['Gold'].row # I also tried: max_gold = row.Gold
return df.max_gold
I had problems how to merge the printing function into the code above, so I added it separately:
1.b:
for row in df.itertuples():
print(row.Gold) # or: print(max_gold)
1.c:
for col in df.columns:
if col[:2]=='Gold':
df[df['Gold'].max()]
2.
def answer_one():
df = pd.DataFrame(columns=['Gold']) # syntax error.
for row in df.itertuples(): # The same as the separated code sction above.
print(row.Gold)
3.
def answer_one():
print(df[['Gold']][df.Value == df.Value.max()]) # I don't know if "Value" is a key word or not.
def answer_one(): return df['Gold'].max() # right syntax, wrong result (not the max value).
5.
def answer_one():
s=data.max()
print '%s' % (s['Gold']) # syntax error.
6.a:
def answer_one():
df.loc[df['Gold'].idxmax()] # right syntax, wrong output (all the column indexes of the table are shown in a column)
6.b:
def answer_one():
df.loc[:,['Gold']] # or: df.loc['Gold']
df['Gold'].max()