dulatus
dulatus

Reputation: 55

Why does pandas.Dataframe.drop() returns None?

Here in my code I read the data from 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:

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

Even dropping by the list of index returns 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():

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

Upvotes: 4

Views: 5611

Answers (1)

jezrael
jezrael

Reputation: 862406

It looks like need 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:

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

Or:

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

Upvotes: 3

Related Questions