简体   繁体   English

为什么pandas.Dataframe.drop()返回None?

[英]Why does pandas.Dataframe.drop() returns None?

Here in my code I read the data from CSV: 在我的代码中,我从CSV中读取数据:

data = pandas.read_csv('dataset/job_functions.csv', names=["job","category"] ,skiprows=1).dropna().reindex()
num_jobs = data["job"].size

Then I want to drop the rows which 'category' label does not equal to i : 然后我要删除“类别”标签不等于i

data = data.drop(data[data.category!=i].index,inplace = True)
print(data.head())

Even dropping by the list of index returns None: 即使删除索引列表也返回None:

data = data.drop(data.index[[1,2,3]],inplace = True)

Error message: 错误信息:

Traceback (most recent call last):
File "sample.py", line 162, in 
  delete_common_words(27)
File "sample.py", line 92, in delete_common_words
  print(data.head())
AttributeError: 'NoneType' object has no attribute 'head'

Here is the data until I use the drop() : 这是直到我使用drop()为止的数据:

                                                 job  category
0  офис   менеджер реализация гербицидовоформлени...         2
1  менеджер   отдел продажа работа с существующий...        27
2  ведущий   бухгалтер работа с вендер и поставщи...         1
3  менеджер   по продажа и продвижение продукт ус...        27
4  юрист   проведение юридический экспертиза прое...        13

It looks like need boolean indexing : 看起来像需要boolean indexing

import pandas as pd

data = pd.DataFrame({'category':['a','b', 'c']})
print (data)
  category
0        a
1        b
2        c

i = 'a'
print (data[data.category != i])
  category
1        b
2        c

print (data[~data.category.isin(['b','c'])])
  category
0        a

And as EdChum explains, if use inplace=True it return None , so you can use: 就像EdChum解释的那样,如果使用EdChum inplace=True则返回None ,因此您可以使用:

#omit inplace=True
data = data.drop(data[data.category!=i].index)

Or: 要么:

#remove assigning
data.drop(data[data.category!=i].index,inplace = True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM