【问题标题】:str[0:z] works with pandas but not with modin: TypeError: 'StringMethods' object is not subscriptablestr[0:z] 适用于 pandas 但不适用于 modin:TypeError: 'StringMethods' object is not subscriptable
【发布时间】:2021-04-05 01:28:32
【问题描述】:

我在 Python 3.7 上运行 Spyder,并且是 modin 的新手。我想检索字符串中的第一个字符并保存到新列。当我用 pandas 正常运行时,它可以工作:

import pandas as pd
data = pd.read_csv('Path/data.csv', dtype=str, encoding='utf-8')
data['FL_x']=data['x'].str[0:3]

但是当我用 modin 运行相同的程序时,我得到了错误:'TypeError: 'StringMethods' object is not subscriptable'

import modin.pandas as pd
#etc.

我可以通过使用 str.get() 来解决问题:

data['FL_x']=data['x'].str.get(0) + data['x'].str.get(1) + data['x'].str.get(2)

但是对于大量数据和检查许多首字符非常耗时。

有没有一种简单的方法可以像使用 pandas 一样使用 modin 立即检索字符串中的前 z 个字符?

【问题讨论】:

    标签: python pandas typeerror modin


    【解决方案1】:

    你可以试试:

    data['FL_x']=data['x'].str.slice(stop=3)
    

    【讨论】:

    • 它有效。非常感谢!普通 pandas 仍然比 modin(使用 ray)快,但比我原来的解决方案要快。
    猜你喜欢
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 2020-12-15
    相关资源
    最近更新 更多