pandas AttributeError: 'DataFrame' 对象没有属性 'label'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50099922/
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
AttributeError: 'DataFrame' object has no attribute 'label'
提问by Wanderer
I created a data frame using the following line:
我使用以下行创建了一个数据框:
df = pd.read_csv('/Users/cs213/Desktop/class1.csv', sep = ',', error_bad_lines=False)
and if print the columns as such
如果这样打印列
print (df.columns)
I get
我得到
Index(['Text', 'label'], dtype='object')
索引(['文本','标签'],dtype='对象')
But if I wanted to use the columns as in here
但是如果我想使用这里的列
df = df[df.Text.apply(lambda x: x.isnumeric())]
df = df[df.Text.apply(lambda x: x !="")]
df = df[df.label.apply(lambda x: x !="")]
I get the following error:
我收到以下错误:
AttributeError: 'DataFrame' object has no attribute 'label'
AttributeError: 'DataFrame' 对象没有属性 'label'
I have already tried the solution in here: Data-frame Object has no Attributeand it did not work.
我已经在这里尝试了解决方案: Data-frame Object has no Attribute并且它不起作用。
Sample of the CSV file
CSV文件示例
采纳答案by pissall
EDIT 1:
编辑 1:
Reproducible sample of your CSV :
CSV 的可重现样本:
df = pd.DataFrame({'Text': [u'Well I am', u"Not my scene", u"Brutal"], 'label': ['y', 'n', 'n']})
The function you are trying to run:
您尝试运行的功能:
>>> df = pd.DataFrame({'Text': [u'Well I am', u"Not my scene", u"Brutal"], 'label': ['y', 'n', 'n']})
>>> df
Text label
0 Well I am y
1 Not my scene n
2 Brutal n
>>> df = df[df['Text'].apply(lambda x: x.isnumeric())]
>>> df
Empty DataFrame
Columns: [Text, label]
Index: []
Of course there will be no attribute 'label'
当然不会有属性“标签”
So what's happening is that, all x.isnumeric()
calls return False
, and hence none of the data is saved to df
. What you are trying to do with df = df[df['Text'].apply(lambda x: x.isnumeric())]
is that "In df, what are the rows in which 'Text' is numeric." (Now this returns False
). None of the rows are numeric, so you get an empty dataframe.
所以发生的事情是,所有x.isnumeric()
调用都返回False
,因此没有任何数据保存到df
. 您想要做的df = df[df['Text'].apply(lambda x: x.isnumeric())]
是“在 df 中,'Text' 是数字的行是什么。” (现在返回False
)。没有一行是数字的,所以你得到一个空的数据框。