Pandas 错误:只能使用带有字符串值的 .str 访问器,它在 Pandas 中使用 np.object_ dtype

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48958708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:14:17  来源:igfitidea点击:

Pandas error: Can only use .str accessor with string values, which use np.object_ dtype in pandas

pythonpandas

提问by saikiran

I have data in my .txtfile as below:

我的.txt文件中有如下数据:

029070 ***** 190101010600 270 36 OVC ** 0.0 ** **

I want to extract 190101 from the column 3, I am getting AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandasbelow is my python pandas. Below is my code

我想从第 3 列中提取 190101,我收到AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandasbelow is my python pandas。下面是我的代码

import pandas as pd
import numpy as np
import re

data = pd.read_csv('dummy.txt', sep=" ", low_memory=False, header=None)
data.columns = ["a", "b", "c","d","e","f","g","h","i","j"]

print(data.c.str[0:6])

回答by Scott Boston

The problem here is that when you read your txt file, in it is casting "c" as an integer and the .str accessor will not work with non-string dtypes, you can fix this problem a couple of ways:

这里的问题是,当您读取 txt 文件时,它将“c”转换为整数,并且 .str 访问器不适用于非字符串 dtypes,您可以通过以下几种方法解决此问题:

Option 1

选项1

Cast the integer as a string in the print statement.

在打印语句中将整数转换为字符串。

print(data.c.astype(str).str[0:6])

0    190101
Name: c, dtype: object

Option 2

选项 2

Cast as a string on the into the dataframe with dtypeparameter in read_csv

使用dtype参数 in将其作为字符串投射到数据框中read_csv

data = pd.read_csv(txtfile, sep=' ', header=None, dtype={2:'str'})
data.columns = list('abcdefghij')
print(data.c.str[0:6]

0    190101
Name: c, dtype: object