首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >列具有dtype对象,不能对此dtype使用方法‘this’。

列具有dtype对象,不能对此dtype使用方法‘this’。
EN

Stack Overflow用户
提问于 2021-12-26 03:23:05
回答 1查看 960关注 0票数 0

我正在使用Google,我想用熊猫分析一个来自Google电子表格的文件。我成功地导入了它们,我可以用pd.DataFrame打印出来。

代码语言:javascript
复制
data_tablet = gc.open_by_url(f'https://docs.google.com/spreadsheets/d/{sheet_id}/edit#gid={tablet_gid}')

tablet_var = data_tablet.worksheet('tablet')
tablet_data = tablet_var.get_all_records()

df_tablet = pd.DataFrame(tablet_data)
print(df_tablet)
代码语言:javascript
复制
                    name  1st quarter  ...  4th quarter     total
0      Albendazol 400 mg         18.0  ...         60.0        78
1      Alopurinol 100 mg        125.0  ...        821.0       946
2        Ambroksol 30 mg        437.0  ...        798.0  1,235.00
3      Aminofilin 200 mg         70.0  ...        522.0       592
4    Amitriptilin 25 mg          83.0  ...        178.0       261
..                   ...          ...  ...          ...       ...
189   Levoflaksin 250 mg        611.0  ...        822.0  1,433.00
190            Linezolid        675.0  ...        315.0       990
191  Moxifloxacin 400 mg        964.0  ...         99.0  1,063.00
192  Pyrazinamide 500 mg        395.0  ...        189.0       584
193          Vitamin B 6        330.0  ...        825.0  1,155.00

[194 rows x 6 columns]

我想从total的194个项目中选择前10名,但它没有工作。

cannot use method 'nlargest' with this dtype中选择前10位的

  • ,然后在下面运行命令,我将得到total

代码语言:javascript
复制
# Ambil data 10 terbesar dari 194 item
df_tablet_top10 = df_tablet.nlargest(10, 'total')
print(df_tablet_top10)
代码语言:javascript
复制
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-7-a7295330f7a9> in <module>()
      1 # Ambil data 10 terbesar dari 194 item
----> 2 df_tablet_top10 = df_tablet.nlargest(10, 'total')
      3 print(df_tablet_top10)

2 frames

/usr/local/lib/python3.7/dist-packages/pandas/core/algorithms.py in compute(self, method)
   1273             if not self.is_valid_dtype_n_method(dtype):
   1274                 raise TypeError(
-> 1275                     f"Column {repr(column)} has dtype {dtype}, "
   1276                     f"cannot use method {repr(method)} with this dtype"
   1277                 )

TypeError: Column 'total' has dtype object, cannot use method 'nlargest' with this dtype

  • ,但是当我从1st quarter中选择它时,它运行得很好,

代码语言:javascript
复制
df_tablet_top10 = df_tablet.nlargest(10, '1st quarter')
print(df_tablet_top10)
代码语言:javascript
复制
                           nama  1st quarter  ...  4th quarter     total
154             Salbutamol 4 mg        981.0  ...         23.0  1,004.00
74   MDT FB dewasa (obat kusta)        978.0  ...        910.0  1,888.00
155   Paracetamol 500 mg Tablet        976.0  ...        503.0  1,479.00
33              Furosemid 40 mg        975.0  ...        524.0  1,499.00
23          Deksametason 0,5 mg        972.0  ...        793.0  1,765.00
21    Bisakodil (dulkolax) 5 mg        970.0  ...        798.0  1,768.00
191         Moxifloxacin 400 mg        964.0  ...         99.0  1,063.00
85          Metronidazol 250 mg        958.0  ...        879.0  1,837.00
96          Nistatin 500.000 IU        951.0  ...        425.0  1,376.00
37             Glimepirid 2 mg         947.0  ...        890.0  1,837.00

[10 rows x 6 columns]

知道是什么导致了这一切吗?

此外,我已经将1st quarter的格式更改为total,作为google工作表上的number,但它仍然不起作用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-26 03:33:16

我找到了解决办法,但没有找到解释。

我所做的只是将total列转换为float

代码语言:javascript
复制
df_tablet['total'] = df_tablet['total'].astype(float)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70484024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档