【问题标题】:pandas: TypeError: reduction operation 'argmax' not allowed for this dtypepandas:TypeError:此dtype不允许减少操作'argmax'
【发布时间】:2020-09-04 21:18:00
【问题描述】:

python 3.7.6 和 jupyter 笔记本

import numpy as np
import pandas as pd

chipo = pd.DataFrame(open('orders_24.csv').read().splitlines())

chipo = chipo[0].str.split('\t',expand=True)

chipo.columns = ['order_id', 'quantity', 'item_name', 'choice_description', 'item_price']

chipo = chipo.drop(labels=0)

chipo

This is chipo

我想找到具有 bigget roder_iditem

I tried many ways but can't use this function

我的方式有什么问题? 或者有什么方法可以找到该 item 中最大的 order_id? 谢谢!

【问题讨论】:

  • 我不清楚您在哪一行代码中遇到了 TypeError。你应该澄清你的问题。

标签: python pandas


【解决方案1】:

您需要更改“order_id”的数据类型。看起来它是一个对象类型。您可以使用 dtypes 验证 dtype。

print(chipo.dtypes)

您无法找到一个对象类型的最大值。可以使用 astype 方法更改列类型,并将列转换为整数类型。

chipo['order_id'] = chipo['order_id'].astype('int') # Change column type to integer
print(chipo['item_name'].iloc[chipo['order_id'].argmax()]) # Print result

请注意,如果您现在检查 dtype,chipo['order_id'] 的 dtype 现在是 int(整数)。

如果您不想永久更改列,可以实施单行解决方案。

print(chipo['item_name'].iloc[chipo['order_id'].astype('int').argmax()])

最后,作为参考,您应该始终了解您正在处理的列类型。这可以为您节省大量的故障排除。

【讨论】:

  • 谢谢!我通过更改 dtype 解决了这个问题。
猜你喜欢
  • 2018-07-21
  • 2012-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-03
  • 2021-02-10
  • 2021-01-01
相关资源
最近更新 更多