python - AttributeError:只能使用带有字符串值的 .str 访问器
问题描述
这不是骗子!我已经解决了所有相关问题,但他们没有回答我的问题。
我正在使用标准方法从 pandas 字符串列中去除空格,如下所述:
df_obj = df.select_dtypes(['object'])
df[df_obj.columns] = df_obj.apply(lambda x: x.str.strip())
但不断得到:
c:\users\user\appdata\local\programs\python\python37\lib\site-packages\pandas\core\strings.py in _validate(data) if inferred_dtype not in allowed_types: raise AttributeError("Can only use .str具有字符串值的访问器!”)
返回 inferred_dtypeAttributeError:只能使用带有字符串值的 .str 访问器!
当我运行时,print(df_obj .dtypes)
我将所有列列为“ object
”。所以有什么问题?
熊猫版本:'1.1.4'
解决方案
通过修改 2. 行将列转换为字符串来解决它:
df[df_obj.columns] = df_obj.apply(lambda x: x.astype('str').str.strip())
我以前从来没有在列上使用过它。object
我认为对象列默认是字符串...
推荐阅读
- angular - Angular Material Progresbar 不使用 *ngif 显示/隐藏
- html - 将默认值设置为 HTML 下拉列表
- c# - Unity ScriptableObject、UnityEvent 和 GenericObject 使用
- nlp - 如何获取词性不变的单词列表
- arduino - 关于通过 USART 上的 MDB 协议与自动售货机通信时 Arduino 的 TX 线的问题
- javascript - 未捕获的 ReferenceError:在 HTMLElement.onclick 中未定义 Oncheckboxclicked
- tomcat8 - 更改 server.xml 后,Tomcat 不会指向新端口
- android - 我们是否有任何机制来跟踪应用程序是否从给定的 URL 安装?
- node.js - 节点中的 process.exitcode 和 process.exit() 有什么区别吗?
- powershell - powershell中的grep gci输出