我有这张桌子:
使用Jupyter Notebook创建的DataFrame表。
这实际上只是表的一部分。
完整的表实际上是一个.csv文件,通过使用.head()函数,仅显示前五行。
我需要编写一个函数,该函数返回并打印第二列中所有值中的最大值,其标签为“ Gold”。
该函数应该返回一个字符串值。
在写问题之前,我查看了多个资料来源,尝试了多种方法来解决我的问题。
这似乎是一个非常简单的解决方案,但是不幸的是我没有成功找到它。
(此查询可能有几种可选的解决方案...?)
请帮助我,我完全感到困惑。
谢谢!
以下是所有来源:
这是我尝试解决此问题的所有方法,其中一些存在语法错误:
1.a:找出最大值的传统算法,例如C语言:“ for”循环。
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
我在将打印功能合并到上面的代码中遇到问题,因此我单独添加了它:
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()
很好的第一个问题,我假设您正在Coursera上进行Python for Datascience课程?
正如已经指出的,df['Gold'].max()
但是正确的是,如果数据类型错误,它将不会返回预期的结果。所以首先要确保它是一个数字。您可以通过运行检查此结果(df['Gold'].dtype
如果输出不是int64
该数据集的数据),则可以通过运行它进行更正df.loc[:,'Gold'] = df.loc[:,'Gold'].str.replace(',','').astype(int)
,df['Gold'].max()
然后返回1022。
对于for循环,在这种情况下,您可以遍历Gold系列中的所有值,而不是遍历所有列和所有行。请注意,python使用0索引!因此,如果您将第1行用作起点,则如果最大值位于第一行(row0)中,并且使用[Index]
和不是进行索引,则会得到错误的结果.Index
。因此,for循环可能看起来像这样。
CurrentMax = df['Gold'][0]
for value in df['Gold']:
if value>CurrentMax:
CurrentMax = value
print(CurrentMax)
包装功能:
def rowbyrow(df=df):
CurrentMax = df['Gold'][0]
for value in df['Gold']:
if value>CurrentMax:
CurrentMax = value
#print(CurrentMax) if you want to print the result when running
return CurrentMax
关于第3点,我相信你是后低于,它过滤通过金黄金在那里的值等于最大值,当你使用前后两个支架Gold
,这将返回一个数据帧,而不仅仅是值:df[['Gold']][df.Gold == df.Gold.max()]
一个支架它会返回一个序列:df['Gold'][df.Gold == df.Gold.max()]
关于第5点,如果您使用的是python 3,可能会导致语法错误?在python 3中,您需要使用()
afterprint
语句,因此以下代码应该起作用:
s=df.max()
print('%s' % (s['Gold']))
关于第6点:a,如果您只想输出特定的列,则需要在过滤条件(用分隔)之后传递该列,,
如下所示:
df.loc[df['Gold'].idxmax(),'Gold']
如果要返回几列,可以传递一个列表,例如
df.loc[df['Gold'].idxmax(),['Country','Gold']]
对于点1:c,[:2]
将返回前两个字母。因此与四字母词相比总是错误的Gold
。
一些性能比较:
1.
%%timeit
df.loc[df['Gold'].idxmax(),'Gold']
10000 loops, best of 3: 76.6 µs per loop
2.
%%timeit
s=df.max()
'%s' % (s['Gold'])
1000 loops, best of 3: 733 µs per loop
3.
%%timeit
rowbyrow()
10000 loops, best of 3: 71 µs per loop
4.
%%timeit
df['Gold'].max()
10000 loops, best of 3: 106 µs per loop
令我惊讶的是,该功能rowbyrow()
取得了最快的效果。
创建具有10k随机值的序列后,rowbyrow()
仍然是最快的。
看这里:
df = pd.DataFrame((np.random.rand(10000, 1)), columns=['Gold'])
%%timeit # no. 1
df['Gold'].max()
The slowest run took 10.30 times longer than the fastest.
10000 loops, best of 3: 127 µs per loop
%%timeit # no. 2
rowbyrow()
The slowest run took 8.12 times longer than the fastest.
10000 loops, best of 3: 72.7 µs per loop
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句